pata_at32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. #define PIO_MASK (0x1f)
  64. /*
  65. * Struct containing private information about device.
  66. */
  67. struct at32_ide_info {
  68. unsigned int irq;
  69. struct resource res_ide;
  70. struct resource res_alt;
  71. void __iomem *ide_addr;
  72. void __iomem *alt_addr;
  73. unsigned int cs;
  74. struct smc_config smc;
  75. };
  76. /*
  77. * Setup SMC for the given ATA timing.
  78. */
  79. static int pata_at32_setup_timing(struct device *dev,
  80. struct at32_ide_info *info,
  81. const struct ata_timing *ata)
  82. {
  83. struct smc_config *smc = &info->smc;
  84. struct smc_timing timing;
  85. int active;
  86. int recover;
  87. memset(&timing, 0, sizeof(struct smc_timing));
  88. /* Total cycle time */
  89. timing.read_cycle = ata->cyc8b;
  90. /* DIOR <= CFIOR timings */
  91. timing.nrd_setup = ata->setup;
  92. timing.nrd_pulse = ata->act8b;
  93. timing.nrd_recover = ata->rec8b;
  94. /* Convert nanosecond timing to clock cycles */
  95. smc_set_timing(smc, &timing);
  96. /* Add one extra cycle setup due to signal ring */
  97. smc->nrd_setup = smc->nrd_setup + 1;
  98. active = smc->nrd_setup + smc->nrd_pulse;
  99. recover = smc->read_cycle - active;
  100. /* Need at least two cycles recovery */
  101. if (recover < 2)
  102. smc->read_cycle = active + 2;
  103. /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
  104. smc->ncs_read_setup = 1;
  105. smc->ncs_read_pulse = smc->read_cycle - 2;
  106. /* Write timings same as read timings */
  107. smc->write_cycle = smc->read_cycle;
  108. smc->nwe_setup = smc->nrd_setup;
  109. smc->nwe_pulse = smc->nrd_pulse;
  110. smc->ncs_write_setup = smc->ncs_read_setup;
  111. smc->ncs_write_pulse = smc->ncs_read_pulse;
  112. /* Do some debugging output of ATA and SMC timings */
  113. dev_dbg(dev, "ATA: C=%d S=%d P=%d R=%d\n",
  114. ata->cyc8b, ata->setup, ata->act8b, ata->rec8b);
  115. dev_dbg(dev, "SMC: C=%d S=%d P=%d NS=%d NP=%d\n",
  116. smc->read_cycle, smc->nrd_setup, smc->nrd_pulse,
  117. smc->ncs_read_setup, smc->ncs_read_pulse);
  118. /* Finally, configure the SMC */
  119. return smc_set_configuration(info->cs, smc);
  120. }
  121. /*
  122. * Procedures for libATA.
  123. */
  124. static void pata_at32_set_piomode(struct ata_port *ap, struct ata_device *adev)
  125. {
  126. struct ata_timing timing;
  127. struct at32_ide_info *info = ap->host->private_data;
  128. int ret;
  129. /* Compute ATA timing */
  130. ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
  131. if (ret) {
  132. dev_warn(ap->dev, "Failed to compute ATA timing %d\n", ret);
  133. return;
  134. }
  135. /* Setup SMC to ATA timing */
  136. ret = pata_at32_setup_timing(ap->dev, info, &timing);
  137. if (ret) {
  138. dev_warn(ap->dev, "Failed to setup ATA timing %d\n", ret);
  139. return;
  140. }
  141. }
  142. static struct scsi_host_template at32_sht = {
  143. ATA_PIO_SHT(DRV_NAME),
  144. };
  145. static struct ata_port_operations at32_port_ops = {
  146. .inherits = &ata_sff_port_ops,
  147. .cable_detect = ata_cable_40wire,
  148. .set_piomode = pata_at32_set_piomode,
  149. };
  150. static int __init pata_at32_init_one(struct device *dev,
  151. struct at32_ide_info *info)
  152. {
  153. struct ata_host *host;
  154. struct ata_port *ap;
  155. host = ata_host_alloc(dev, 1);
  156. if (!host)
  157. return -ENOMEM;
  158. ap = host->ports[0];
  159. /* Setup ATA bindings */
  160. ap->ops = &at32_port_ops;
  161. ap->pio_mask = PIO_MASK;
  162. ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_SLAVE_POSS;
  163. /*
  164. * Since all 8-bit taskfile transfers has to go on the lower
  165. * byte of the data bus and there is a bug in the SMC that
  166. * makes it impossible to alter the bus width during runtime,
  167. * we need to hardwire the address signals as follows:
  168. *
  169. * A_IDE(2:0) <= A_EBI(3:1)
  170. *
  171. * This makes all addresses on the EBI even, thus all data
  172. * will be on the lower byte of the data bus. All addresses
  173. * used by libATA need to be altered according to this.
  174. */
  175. ap->ioaddr.altstatus_addr = info->alt_addr + (0x06 << 1);
  176. ap->ioaddr.ctl_addr = info->alt_addr + (0x06 << 1);
  177. ap->ioaddr.data_addr = info->ide_addr + (ATA_REG_DATA << 1);
  178. ap->ioaddr.error_addr = info->ide_addr + (ATA_REG_ERR << 1);
  179. ap->ioaddr.feature_addr = info->ide_addr + (ATA_REG_FEATURE << 1);
  180. ap->ioaddr.nsect_addr = info->ide_addr + (ATA_REG_NSECT << 1);
  181. ap->ioaddr.lbal_addr = info->ide_addr + (ATA_REG_LBAL << 1);
  182. ap->ioaddr.lbam_addr = info->ide_addr + (ATA_REG_LBAM << 1);
  183. ap->ioaddr.lbah_addr = info->ide_addr + (ATA_REG_LBAH << 1);
  184. ap->ioaddr.device_addr = info->ide_addr + (ATA_REG_DEVICE << 1);
  185. ap->ioaddr.status_addr = info->ide_addr + (ATA_REG_STATUS << 1);
  186. ap->ioaddr.command_addr = info->ide_addr + (ATA_REG_CMD << 1);
  187. /* Set info as private data of ATA host */
  188. host->private_data = info;
  189. /* Register ATA device and return */
  190. return ata_host_activate(host, info->irq, ata_sff_interrupt,
  191. IRQF_SHARED | IRQF_TRIGGER_RISING,
  192. &at32_sht);
  193. }
  194. /*
  195. * This function may come in handy for people analyzing their own
  196. * EBI -> PATA adaptors.
  197. */
  198. #ifdef DEBUG_BUS
  199. static void __init pata_at32_debug_bus(struct device *dev,
  200. struct at32_ide_info *info)
  201. {
  202. const int d1 = 0xff;
  203. const int d2 = 0x00;
  204. int i;
  205. /* Write 8-bit values (registers) */
  206. iowrite8(d1, info->alt_addr + (0x06 << 1));
  207. iowrite8(d2, info->alt_addr + (0x06 << 1));
  208. for (i = 0; i < 8; i++) {
  209. iowrite8(d1, info->ide_addr + (i << 1));
  210. iowrite8(d2, info->ide_addr + (i << 1));
  211. }
  212. /* Write 16 bit values (data) */
  213. iowrite16(d1, info->ide_addr);
  214. iowrite16(d1 << 8, info->ide_addr);
  215. iowrite16(d1, info->ide_addr);
  216. iowrite16(d1 << 8, info->ide_addr);
  217. }
  218. #endif
  219. static int __init pata_at32_probe(struct platform_device *pdev)
  220. {
  221. const struct ata_timing initial_timing =
  222. {XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0};
  223. struct device *dev = &pdev->dev;
  224. struct at32_ide_info *info;
  225. struct ide_platform_data *board = pdev->dev.platform_data;
  226. struct resource *res;
  227. int irq;
  228. int ret;
  229. if (!board)
  230. return -ENXIO;
  231. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  232. if (!res)
  233. return -ENXIO;
  234. /* Retrive IRQ */
  235. irq = platform_get_irq(pdev, 0);
  236. if (irq < 0)
  237. return irq;
  238. /* Setup struct containing private information */
  239. info = kzalloc(sizeof(struct at32_ide_info), GFP_KERNEL);
  240. if (!info)
  241. return -ENOMEM;
  242. info->irq = irq;
  243. info->cs = board->cs;
  244. /* Request memory resources */
  245. info->res_ide.start = res->start + CF_IDE_OFFSET;
  246. info->res_ide.end = info->res_ide.start + CF_RES_SIZE - 1;
  247. info->res_ide.name = "ide";
  248. info->res_ide.flags = IORESOURCE_MEM;
  249. ret = request_resource(res, &info->res_ide);
  250. if (ret)
  251. goto err_req_res_ide;
  252. info->res_alt.start = res->start + CF_ALT_IDE_OFFSET;
  253. info->res_alt.end = info->res_alt.start + CF_RES_SIZE - 1;
  254. info->res_alt.name = "alt";
  255. info->res_alt.flags = IORESOURCE_MEM;
  256. ret = request_resource(res, &info->res_alt);
  257. if (ret)
  258. goto err_req_res_alt;
  259. /* Setup non-timing elements of SMC */
  260. info->smc.bus_width = 2; /* 16 bit data bus */
  261. info->smc.nrd_controlled = 1; /* Sample data on rising edge of NRD */
  262. info->smc.nwe_controlled = 0; /* Drive data on falling edge of NCS */
  263. info->smc.nwait_mode = 3; /* NWAIT is in READY mode */
  264. info->smc.byte_write = 0; /* Byte select access type */
  265. info->smc.tdf_mode = 0; /* TDF optimization disabled */
  266. info->smc.tdf_cycles = 0; /* No TDF wait cycles */
  267. /* Setup SMC to ATA timing */
  268. ret = pata_at32_setup_timing(dev, info, &initial_timing);
  269. if (ret)
  270. goto err_setup_timing;
  271. /* Map ATA address space */
  272. ret = -ENOMEM;
  273. info->ide_addr = devm_ioremap(dev, info->res_ide.start, 16);
  274. info->alt_addr = devm_ioremap(dev, info->res_alt.start, 16);
  275. if (!info->ide_addr || !info->alt_addr)
  276. goto err_ioremap;
  277. #ifdef DEBUG_BUS
  278. pata_at32_debug_bus(dev, info);
  279. #endif
  280. /* Setup and register ATA device */
  281. ret = pata_at32_init_one(dev, info);
  282. if (ret)
  283. goto err_ata_device;
  284. return 0;
  285. err_ata_device:
  286. err_ioremap:
  287. err_setup_timing:
  288. release_resource(&info->res_alt);
  289. err_req_res_alt:
  290. release_resource(&info->res_ide);
  291. err_req_res_ide:
  292. kfree(info);
  293. return ret;
  294. }
  295. static int __exit pata_at32_remove(struct platform_device *pdev)
  296. {
  297. struct ata_host *host = platform_get_drvdata(pdev);
  298. struct at32_ide_info *info;
  299. if (!host)
  300. return 0;
  301. info = host->private_data;
  302. ata_host_detach(host);
  303. if (!info)
  304. return 0;
  305. release_resource(&info->res_ide);
  306. release_resource(&info->res_alt);
  307. kfree(info);
  308. return 0;
  309. }
  310. /* work with hotplug and coldplug */
  311. MODULE_ALIAS("platform:at32_ide");
  312. static struct platform_driver pata_at32_driver = {
  313. .remove = __exit_p(pata_at32_remove),
  314. .driver = {
  315. .name = "at32_ide",
  316. .owner = THIS_MODULE,
  317. },
  318. };
  319. static int __init pata_at32_init(void)
  320. {
  321. return platform_driver_probe(&pata_at32_driver, pata_at32_probe);
  322. }
  323. static void __exit pata_at32_exit(void)
  324. {
  325. platform_driver_unregister(&pata_at32_driver);
  326. }
  327. module_init(pata_at32_init);
  328. module_exit(pata_at32_exit);
  329. MODULE_LICENSE("GPL");
  330. MODULE_DESCRIPTION("AVR32 SMC/CFC PATA Driver");
  331. MODULE_AUTHOR("Kristoffer Nyborg Gregertsen <kngregertsen@norway.atmel.com>");
  332. MODULE_VERSION(DRV_VERSION);