pata_at32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * AVR32 SMC/CFC PATA Driver
  3. *
  4. * Copyright (C) 2007 Atmel Norway
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. */
  10. #define DEBUG
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irq.h>
  19. #include <scsi/scsi_host.h>
  20. #include <linux/ata.h>
  21. #include <linux/libata.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <mach/board.h>
  25. #include <mach/smc.h>
  26. #define DRV_NAME "pata_at32"
  27. #define DRV_VERSION "0.0.3"
  28. /*
  29. * CompactFlash controller memory layout relative to the base address:
  30. *
  31. * Attribute memory: 0000 0000 -> 003f ffff
  32. * Common memory: 0040 0000 -> 007f ffff
  33. * I/O memory: 0080 0000 -> 00bf ffff
  34. * True IDE Mode: 00c0 0000 -> 00df ffff
  35. * Alt IDE Mode: 00e0 0000 -> 00ff ffff
  36. *
  37. * Only True IDE and Alt True IDE mode are needed for this driver.
  38. *
  39. * True IDE mode => CS0 = 0, CS1 = 1 (cmd, error, stat, etc)
  40. * Alt True IDE mode => CS0 = 1, CS1 = 0 (ctl, alt_stat)
  41. */
  42. #define CF_IDE_OFFSET 0x00c00000
  43. #define CF_ALT_IDE_OFFSET 0x00e00000
  44. #define CF_RES_SIZE 2048
  45. /*
  46. * Define DEBUG_BUS if you are doing debugging of your own EBI -> PATA
  47. * adaptor with a logic analyzer or similar.
  48. */
  49. #undef DEBUG_BUS
  50. /*
  51. * ATA PIO modes
  52. *
  53. * Name | Mb/s | Min cycle time | Mask
  54. * --------+-------+----------------+--------
  55. * Mode 0 | 3.3 | 600 ns | 0x01
  56. * Mode 1 | 5.2 | 383 ns | 0x03
  57. * Mode 2 | 8.3 | 240 ns | 0x07
  58. * Mode 3 | 11.1 | 180 ns | 0x0f
  59. * Mode 4 | 16.7 | 120 ns | 0x1f
  60. *
  61. * Alter PIO_MASK below according to table to set maximal PIO mode.
  62. */
  63. enum {
  64. PIO_MASK = ATA_PIO4,
  65. };
  66. /*
  67. * Struct containing private information about device.
  68. */
  69. struct at32_ide_info {
  70. unsigned int irq;
  71. struct resource res_ide;
  72. struct resource res_alt;
  73. void __iomem *ide_addr;
  74. void __iomem *alt_addr;
  75. unsigned int cs;
  76. struct smc_config smc;
  77. };
  78. /*
  79. * Setup SMC for the given ATA timing.
  80. */
  81. static int pata_at32_setup_timing(struct device *dev,
  82. struct at32_ide_info *info,
  83. const struct ata_timing *ata)
  84. {
  85. struct smc_config *smc = &info->smc;
  86. struct smc_timing timing;
  87. int active;
  88. int recover;
  89. memset(&timing, 0, sizeof(struct smc_timing));
  90. /* Total cycle time */
  91. timing.read_cycle = ata->cyc8b;
  92. /* DIOR <= CFIOR timings */
  93. timing.nrd_setup = ata->setup;
  94. timing.nrd_pulse = ata->act8b;
  95. timing.nrd_recover = ata->rec8b;
  96. /* Convert nanosecond timing to clock cycles */
  97. smc_set_timing(smc, &timing);
  98. /* Add one extra cycle setup due to signal ring */
  99. smc->nrd_setup = smc->nrd_setup + 1;
  100. active = smc->nrd_setup + smc->nrd_pulse;
  101. recover = smc->read_cycle - active;
  102. /* Need at least two cycles recovery */
  103. if (recover < 2)
  104. smc->read_cycle = active + 2;
  105. /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
  106. smc->ncs_read_setup = 1;
  107. smc->ncs_read_pulse = smc->read_cycle - 2;
  108. /* Write timings same as read timings */
  109. smc->write_cycle = smc->read_cycle;
  110. smc->nwe_setup = smc->nrd_setup;
  111. smc->nwe_pulse = smc->nrd_pulse;
  112. smc->ncs_write_setup = smc->ncs_read_setup;
  113. smc->ncs_write_pulse = smc->ncs_read_pulse;
  114. /* Do some debugging output of ATA and SMC timings */
  115. dev_dbg(dev, "ATA: C=%d S=%d P=%d R=%d\n",
  116. ata->cyc8b, ata->setup, ata->act8b, ata->rec8b);
  117. dev_dbg(dev, "SMC: C=%d S=%d P=%d NS=%d NP=%d\n",
  118. smc->read_cycle, smc->nrd_setup, smc->nrd_pulse,
  119. smc->ncs_read_setup, smc->ncs_read_pulse);
  120. /* Finally, configure the SMC */
  121. return smc_set_configuration(info->cs, smc);
  122. }
  123. /*
  124. * Procedures for libATA.
  125. */
  126. static void pata_at32_set_piomode(struct ata_port *ap, struct ata_device *adev)
  127. {
  128. struct ata_timing timing;
  129. struct at32_ide_info *info = ap->host->private_data;
  130. int ret;
  131. /* Compute ATA timing */
  132. ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
  133. if (ret) {
  134. dev_warn(ap->dev, "Failed to compute ATA timing %d\n", ret);
  135. return;
  136. }
  137. /* Setup SMC to ATA timing */
  138. ret = pata_at32_setup_timing(ap->dev, info, &timing);
  139. if (ret) {
  140. dev_warn(ap->dev, "Failed to setup ATA timing %d\n", ret);
  141. return;
  142. }
  143. }
  144. static struct scsi_host_template at32_sht = {
  145. ATA_PIO_SHT(DRV_NAME),
  146. };
  147. static struct ata_port_operations at32_port_ops = {
  148. .inherits = &ata_sff_port_ops,
  149. .cable_detect = ata_cable_40wire,
  150. .set_piomode = pata_at32_set_piomode,
  151. };
  152. static int __init pata_at32_init_one(struct device *dev,
  153. struct at32_ide_info *info)
  154. {
  155. struct ata_host *host;
  156. struct ata_port *ap;
  157. host = ata_host_alloc(dev, 1);
  158. if (!host)
  159. return -ENOMEM;
  160. ap = host->ports[0];
  161. /* Setup ATA bindings */
  162. ap->ops = &at32_port_ops;
  163. ap->pio_mask = PIO_MASK;
  164. ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_SLAVE_POSS;
  165. /*
  166. * Since all 8-bit taskfile transfers has to go on the lower
  167. * byte of the data bus and there is a bug in the SMC that
  168. * makes it impossible to alter the bus width during runtime,
  169. * we need to hardwire the address signals as follows:
  170. *
  171. * A_IDE(2:0) <= A_EBI(3:1)
  172. *
  173. * This makes all addresses on the EBI even, thus all data
  174. * will be on the lower byte of the data bus. All addresses
  175. * used by libATA need to be altered according to this.
  176. */
  177. ap->ioaddr.altstatus_addr = info->alt_addr + (0x06 << 1);
  178. ap->ioaddr.ctl_addr = info->alt_addr + (0x06 << 1);
  179. ap->ioaddr.data_addr = info->ide_addr + (ATA_REG_DATA << 1);
  180. ap->ioaddr.error_addr = info->ide_addr + (ATA_REG_ERR << 1);
  181. ap->ioaddr.feature_addr = info->ide_addr + (ATA_REG_FEATURE << 1);
  182. ap->ioaddr.nsect_addr = info->ide_addr + (ATA_REG_NSECT << 1);
  183. ap->ioaddr.lbal_addr = info->ide_addr + (ATA_REG_LBAL << 1);
  184. ap->ioaddr.lbam_addr = info->ide_addr + (ATA_REG_LBAM << 1);
  185. ap->ioaddr.lbah_addr = info->ide_addr + (ATA_REG_LBAH << 1);
  186. ap->ioaddr.device_addr = info->ide_addr + (ATA_REG_DEVICE << 1);
  187. ap->ioaddr.status_addr = info->ide_addr + (ATA_REG_STATUS << 1);
  188. ap->ioaddr.command_addr = info->ide_addr + (ATA_REG_CMD << 1);
  189. /* Set info as private data of ATA host */
  190. host->private_data = info;
  191. /* Register ATA device and return */
  192. return ata_host_activate(host, info->irq, ata_sff_interrupt,
  193. IRQF_SHARED | IRQF_TRIGGER_RISING,
  194. &at32_sht);
  195. }
  196. /*
  197. * This function may come in handy for people analyzing their own
  198. * EBI -> PATA adaptors.
  199. */
  200. #ifdef DEBUG_BUS
  201. static void __init pata_at32_debug_bus(struct device *dev,
  202. struct at32_ide_info *info)
  203. {
  204. const int d1 = 0xff;
  205. const int d2 = 0x00;
  206. int i;
  207. /* Write 8-bit values (registers) */
  208. iowrite8(d1, info->alt_addr + (0x06 << 1));
  209. iowrite8(d2, info->alt_addr + (0x06 << 1));
  210. for (i = 0; i < 8; i++) {
  211. iowrite8(d1, info->ide_addr + (i << 1));
  212. iowrite8(d2, info->ide_addr + (i << 1));
  213. }
  214. /* Write 16 bit values (data) */
  215. iowrite16(d1, info->ide_addr);
  216. iowrite16(d1 << 8, info->ide_addr);
  217. iowrite16(d1, info->ide_addr);
  218. iowrite16(d1 << 8, info->ide_addr);
  219. }
  220. #endif
  221. static int __init pata_at32_probe(struct platform_device *pdev)
  222. {
  223. const struct ata_timing initial_timing =
  224. {XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0};
  225. struct device *dev = &pdev->dev;
  226. struct at32_ide_info *info;
  227. struct ide_platform_data *board = pdev->dev.platform_data;
  228. struct resource *res;
  229. int irq;
  230. int ret;
  231. if (!board)
  232. return -ENXIO;
  233. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  234. if (!res)
  235. return -ENXIO;
  236. /* Retrive IRQ */
  237. irq = platform_get_irq(pdev, 0);
  238. if (irq < 0)
  239. return irq;
  240. /* Setup struct containing private information */
  241. info = kzalloc(sizeof(struct at32_ide_info), GFP_KERNEL);
  242. if (!info)
  243. return -ENOMEM;
  244. info->irq = irq;
  245. info->cs = board->cs;
  246. /* Request memory resources */
  247. info->res_ide.start = res->start + CF_IDE_OFFSET;
  248. info->res_ide.end = info->res_ide.start + CF_RES_SIZE - 1;
  249. info->res_ide.name = "ide";
  250. info->res_ide.flags = IORESOURCE_MEM;
  251. ret = request_resource(res, &info->res_ide);
  252. if (ret)
  253. goto err_req_res_ide;
  254. info->res_alt.start = res->start + CF_ALT_IDE_OFFSET;
  255. info->res_alt.end = info->res_alt.start + CF_RES_SIZE - 1;
  256. info->res_alt.name = "alt";
  257. info->res_alt.flags = IORESOURCE_MEM;
  258. ret = request_resource(res, &info->res_alt);
  259. if (ret)
  260. goto err_req_res_alt;
  261. /* Setup non-timing elements of SMC */
  262. info->smc.bus_width = 2; /* 16 bit data bus */
  263. info->smc.nrd_controlled = 1; /* Sample data on rising edge of NRD */
  264. info->smc.nwe_controlled = 0; /* Drive data on falling edge of NCS */
  265. info->smc.nwait_mode = 3; /* NWAIT is in READY mode */
  266. info->smc.byte_write = 0; /* Byte select access type */
  267. info->smc.tdf_mode = 0; /* TDF optimization disabled */
  268. info->smc.tdf_cycles = 0; /* No TDF wait cycles */
  269. /* Setup SMC to ATA timing */
  270. ret = pata_at32_setup_timing(dev, info, &initial_timing);
  271. if (ret)
  272. goto err_setup_timing;
  273. /* Map ATA address space */
  274. ret = -ENOMEM;
  275. info->ide_addr = devm_ioremap(dev, info->res_ide.start, 16);
  276. info->alt_addr = devm_ioremap(dev, info->res_alt.start, 16);
  277. if (!info->ide_addr || !info->alt_addr)
  278. goto err_ioremap;
  279. #ifdef DEBUG_BUS
  280. pata_at32_debug_bus(dev, info);
  281. #endif
  282. /* Setup and register ATA device */
  283. ret = pata_at32_init_one(dev, info);
  284. if (ret)
  285. goto err_ata_device;
  286. return 0;
  287. err_ata_device:
  288. err_ioremap:
  289. err_setup_timing:
  290. release_resource(&info->res_alt);
  291. err_req_res_alt:
  292. release_resource(&info->res_ide);
  293. err_req_res_ide:
  294. kfree(info);
  295. return ret;
  296. }
  297. static int __exit pata_at32_remove(struct platform_device *pdev)
  298. {
  299. struct ata_host *host = platform_get_drvdata(pdev);
  300. struct at32_ide_info *info;
  301. if (!host)
  302. return 0;
  303. info = host->private_data;
  304. ata_host_detach(host);
  305. if (!info)
  306. return 0;
  307. release_resource(&info->res_ide);
  308. release_resource(&info->res_alt);
  309. kfree(info);
  310. return 0;
  311. }
  312. /* work with hotplug and coldplug */
  313. MODULE_ALIAS("platform:at32_ide");
  314. static struct platform_driver pata_at32_driver = {
  315. .remove = __exit_p(pata_at32_remove),
  316. .driver = {
  317. .name = "at32_ide",
  318. .owner = THIS_MODULE,
  319. },
  320. };
  321. static int __init pata_at32_init(void)
  322. {
  323. return platform_driver_probe(&pata_at32_driver, pata_at32_probe);
  324. }
  325. static void __exit pata_at32_exit(void)
  326. {
  327. platform_driver_unregister(&pata_at32_driver);
  328. }
  329. module_init(pata_at32_init);
  330. module_exit(pata_at32_exit);
  331. MODULE_LICENSE("GPL");
  332. MODULE_DESCRIPTION("AVR32 SMC/CFC PATA Driver");
  333. MODULE_AUTHOR("Kristoffer Nyborg Gregertsen <kngregertsen@norway.atmel.com>");
  334. MODULE_VERSION(DRV_VERSION);