pata_buddha.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Buddha, Catweasel and X-Surf PATA controller driver
  4. *
  5. * Copyright (c) 2018 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com
  7. *
  8. * Based on buddha.c:
  9. *
  10. * Copyright (C) 1997, 2001 by Geert Uytterhoeven and others
  11. */
  12. #include <linux/ata.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel.h>
  17. #include <linux/libata.h>
  18. #include <linux/mm.h>
  19. #include <linux/mod_devicetable.h>
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/zorro.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/scsi_host.h>
  25. #include <asm/amigahw.h>
  26. #include <asm/amigaints.h>
  27. #include <asm/ide.h>
  28. #include <asm/setup.h>
  29. #define DRV_NAME "pata_buddha"
  30. #define DRV_VERSION "0.1.1"
  31. #define BUDDHA_BASE1 0x800
  32. #define BUDDHA_BASE2 0xa00
  33. #define BUDDHA_BASE3 0xc00
  34. #define XSURF_BASE1 0xb000 /* 2.5" interface */
  35. #define XSURF_BASE2 0xd000 /* 3.5" interface */
  36. #define BUDDHA_CONTROL 0x11a
  37. #define BUDDHA_IRQ 0xf00
  38. #define XSURF_IRQ 0x7e
  39. #define BUDDHA_IRQ_MR 0xfc0 /* master interrupt enable */
  40. enum {
  41. BOARD_BUDDHA = 0,
  42. BOARD_CATWEASEL,
  43. BOARD_XSURF
  44. };
  45. static unsigned int buddha_bases[3] = {
  46. BUDDHA_BASE1, BUDDHA_BASE2, BUDDHA_BASE3
  47. };
  48. static unsigned int xsurf_bases[2] = {
  49. XSURF_BASE1, XSURF_BASE2
  50. };
  51. static struct scsi_host_template pata_buddha_sht = {
  52. ATA_PIO_SHT(DRV_NAME),
  53. };
  54. /* FIXME: is this needed? */
  55. static unsigned int pata_buddha_data_xfer(struct ata_queued_cmd *qc,
  56. unsigned char *buf,
  57. unsigned int buflen, int rw)
  58. {
  59. struct ata_device *dev = qc->dev;
  60. struct ata_port *ap = dev->link->ap;
  61. void __iomem *data_addr = ap->ioaddr.data_addr;
  62. unsigned int words = buflen >> 1;
  63. /* Transfer multiple of 2 bytes */
  64. if (rw == READ)
  65. raw_insw((u16 *)data_addr, (u16 *)buf, words);
  66. else
  67. raw_outsw((u16 *)data_addr, (u16 *)buf, words);
  68. /* Transfer trailing byte, if any. */
  69. if (unlikely(buflen & 0x01)) {
  70. unsigned char pad[2] = { };
  71. /* Point buf to the tail of buffer */
  72. buf += buflen - 1;
  73. if (rw == READ) {
  74. raw_insw((u16 *)data_addr, (u16 *)pad, 1);
  75. *buf = pad[0];
  76. } else {
  77. pad[0] = *buf;
  78. raw_outsw((u16 *)data_addr, (u16 *)pad, 1);
  79. }
  80. words++;
  81. }
  82. return words << 1;
  83. }
  84. /*
  85. * Provide our own set_mode() as we don't want to change anything that has
  86. * already been configured..
  87. */
  88. static int pata_buddha_set_mode(struct ata_link *link,
  89. struct ata_device **unused)
  90. {
  91. struct ata_device *dev;
  92. ata_for_each_dev(dev, link, ENABLED) {
  93. /* We don't really care */
  94. dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
  95. dev->xfer_shift = ATA_SHIFT_PIO;
  96. dev->flags |= ATA_DFLAG_PIO;
  97. ata_dev_info(dev, "configured for PIO\n");
  98. }
  99. return 0;
  100. }
  101. static bool pata_buddha_irq_check(struct ata_port *ap)
  102. {
  103. u8 ch;
  104. ch = z_readb((unsigned long)ap->private_data);
  105. return !!(ch & 0x80);
  106. }
  107. static void pata_xsurf_irq_clear(struct ata_port *ap)
  108. {
  109. z_writeb(0, (unsigned long)ap->private_data);
  110. }
  111. static struct ata_port_operations pata_buddha_ops = {
  112. .inherits = &ata_sff_port_ops,
  113. .sff_data_xfer = pata_buddha_data_xfer,
  114. .sff_irq_check = pata_buddha_irq_check,
  115. .cable_detect = ata_cable_unknown,
  116. .set_mode = pata_buddha_set_mode,
  117. };
  118. static struct ata_port_operations pata_xsurf_ops = {
  119. .inherits = &ata_sff_port_ops,
  120. .sff_data_xfer = pata_buddha_data_xfer,
  121. .sff_irq_check = pata_buddha_irq_check,
  122. .sff_irq_clear = pata_xsurf_irq_clear,
  123. .cable_detect = ata_cable_unknown,
  124. .set_mode = pata_buddha_set_mode,
  125. };
  126. static int pata_buddha_probe(struct zorro_dev *z,
  127. const struct zorro_device_id *ent)
  128. {
  129. static const char * const board_name[] = {
  130. "Buddha", "Catweasel", "X-Surf"
  131. };
  132. struct ata_host *host;
  133. void __iomem *buddha_board;
  134. unsigned long board;
  135. unsigned int type = ent->driver_data;
  136. unsigned int nr_ports = (type == BOARD_CATWEASEL) ? 3 : 2;
  137. void *old_drvdata;
  138. int i;
  139. dev_info(&z->dev, "%s IDE controller\n", board_name[type]);
  140. board = z->resource.start;
  141. if (type != BOARD_XSURF) {
  142. if (!devm_request_mem_region(&z->dev,
  143. board + BUDDHA_BASE1,
  144. 0x800, DRV_NAME))
  145. return -ENXIO;
  146. } else {
  147. if (!devm_request_mem_region(&z->dev,
  148. board + XSURF_BASE1,
  149. 0x1000, DRV_NAME))
  150. return -ENXIO;
  151. if (!devm_request_mem_region(&z->dev,
  152. board + XSURF_BASE2,
  153. 0x1000, DRV_NAME)) {
  154. }
  155. }
  156. /* Workaround for X-Surf: Save drvdata in case zorro8390 has set it */
  157. if (type == BOARD_XSURF)
  158. old_drvdata = dev_get_drvdata(&z->dev);
  159. /* allocate host */
  160. host = ata_host_alloc(&z->dev, nr_ports);
  161. if (type == BOARD_XSURF)
  162. dev_set_drvdata(&z->dev, old_drvdata);
  163. if (!host)
  164. return -ENXIO;
  165. buddha_board = ZTWO_VADDR(board);
  166. /* enable the board IRQ on Buddha/Catweasel */
  167. if (type != BOARD_XSURF)
  168. z_writeb(0, buddha_board + BUDDHA_IRQ_MR);
  169. for (i = 0; i < nr_ports; i++) {
  170. struct ata_port *ap = host->ports[i];
  171. void __iomem *base, *irqport;
  172. unsigned long ctl = 0;
  173. if (type != BOARD_XSURF) {
  174. ap->ops = &pata_buddha_ops;
  175. base = buddha_board + buddha_bases[i];
  176. ctl = BUDDHA_CONTROL;
  177. irqport = buddha_board + BUDDHA_IRQ + i * 0x40;
  178. } else {
  179. ap->ops = &pata_xsurf_ops;
  180. base = buddha_board + xsurf_bases[i];
  181. /* X-Surf has no CS1* (Control/AltStat) */
  182. irqport = buddha_board + XSURF_IRQ;
  183. }
  184. ap->pio_mask = ATA_PIO4;
  185. ap->flags |= ATA_FLAG_SLAVE_POSS | ATA_FLAG_NO_IORDY;
  186. ap->ioaddr.data_addr = base;
  187. ap->ioaddr.error_addr = base + 2 + 1 * 4;
  188. ap->ioaddr.feature_addr = base + 2 + 1 * 4;
  189. ap->ioaddr.nsect_addr = base + 2 + 2 * 4;
  190. ap->ioaddr.lbal_addr = base + 2 + 3 * 4;
  191. ap->ioaddr.lbam_addr = base + 2 + 4 * 4;
  192. ap->ioaddr.lbah_addr = base + 2 + 5 * 4;
  193. ap->ioaddr.device_addr = base + 2 + 6 * 4;
  194. ap->ioaddr.status_addr = base + 2 + 7 * 4;
  195. ap->ioaddr.command_addr = base + 2 + 7 * 4;
  196. if (ctl) {
  197. ap->ioaddr.altstatus_addr = base + ctl;
  198. ap->ioaddr.ctl_addr = base + ctl;
  199. }
  200. ap->private_data = (void *)irqport;
  201. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", board,
  202. ctl ? board + buddha_bases[i] + ctl : 0);
  203. }
  204. ata_host_activate(host, IRQ_AMIGA_PORTS, ata_sff_interrupt,
  205. IRQF_SHARED, &pata_buddha_sht);
  206. return 0;
  207. }
  208. static void pata_buddha_remove(struct zorro_dev *z)
  209. {
  210. struct ata_host *host = dev_get_drvdata(&z->dev);
  211. ata_host_detach(host);
  212. }
  213. static const struct zorro_device_id pata_buddha_zorro_tbl[] = {
  214. { ZORRO_PROD_INDIVIDUAL_COMPUTERS_BUDDHA, BOARD_BUDDHA},
  215. { ZORRO_PROD_INDIVIDUAL_COMPUTERS_CATWEASEL, BOARD_CATWEASEL},
  216. { 0 }
  217. };
  218. MODULE_DEVICE_TABLE(zorro, pata_buddha_zorro_tbl);
  219. static struct zorro_driver pata_buddha_driver = {
  220. .name = "pata_buddha",
  221. .id_table = pata_buddha_zorro_tbl,
  222. .probe = pata_buddha_probe,
  223. .remove = pata_buddha_remove,
  224. };
  225. /*
  226. * We cannot have a modalias for X-Surf boards, as it competes with the
  227. * zorro8390 network driver. As a stopgap measure until we have proper
  228. * MFD support for this board, we manually attach to it late after Zorro
  229. * has enumerated its boards.
  230. */
  231. static int __init pata_buddha_late_init(void)
  232. {
  233. struct zorro_dev *z = NULL;
  234. /* Auto-bind to regular boards */
  235. zorro_register_driver(&pata_buddha_driver);
  236. /* Manually bind to all X-Surf boards */
  237. while ((z = zorro_find_device(ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, z))) {
  238. static struct zorro_device_id xsurf_ent = {
  239. ZORRO_PROD_INDIVIDUAL_COMPUTERS_X_SURF, BOARD_XSURF
  240. };
  241. pata_buddha_probe(z, &xsurf_ent);
  242. }
  243. return 0;
  244. }
  245. late_initcall(pata_buddha_late_init);
  246. MODULE_AUTHOR("Bartlomiej Zolnierkiewicz");
  247. MODULE_DESCRIPTION("low-level driver for Buddha/Catweasel/X-Surf PATA");
  248. MODULE_LICENSE("GPL v2");
  249. MODULE_VERSION(DRV_VERSION);