pata_buddha.c 7.4 KB

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