pata_at32.c 10 KB

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