pata_at32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 <asm/arch/board.h>
  25. #include <asm/arch/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 void pata_at32_irq_clear(struct ata_port *ap)
  143. {
  144. /* No DMA controller yet */
  145. }
  146. static struct scsi_host_template at32_sht = {
  147. .module = THIS_MODULE,
  148. .name = DRV_NAME,
  149. .ioctl = ata_scsi_ioctl,
  150. .queuecommand = ata_scsi_queuecmd,
  151. .can_queue = ATA_DEF_QUEUE,
  152. .this_id = ATA_SHT_THIS_ID,
  153. .sg_tablesize = LIBATA_MAX_PRD,
  154. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  155. .emulated = ATA_SHT_EMULATED,
  156. .use_clustering = ATA_SHT_USE_CLUSTERING,
  157. .proc_name = DRV_NAME,
  158. .dma_boundary = ATA_DMA_BOUNDARY,
  159. .slave_configure = ata_scsi_slave_config,
  160. .slave_destroy = ata_scsi_slave_destroy,
  161. .bios_param = ata_std_bios_param,
  162. };
  163. static struct ata_port_operations at32_port_ops = {
  164. .set_piomode = pata_at32_set_piomode,
  165. .tf_load = ata_tf_load,
  166. .tf_read = ata_tf_read,
  167. .exec_command = ata_exec_command,
  168. .check_status = ata_check_status,
  169. .dev_select = ata_std_dev_select,
  170. .freeze = ata_bmdma_freeze,
  171. .thaw = ata_bmdma_thaw,
  172. .error_handler = ata_bmdma_error_handler,
  173. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  174. .cable_detect = ata_cable_40wire,
  175. .qc_prep = ata_qc_prep,
  176. .qc_issue = ata_qc_issue_prot,
  177. .data_xfer = ata_data_xfer,
  178. .irq_clear = pata_at32_irq_clear,
  179. .irq_on = ata_irq_on,
  180. .port_start = ata_sff_port_start,
  181. };
  182. static int __init pata_at32_init_one(struct device *dev,
  183. struct at32_ide_info *info)
  184. {
  185. struct ata_host *host;
  186. struct ata_port *ap;
  187. host = ata_host_alloc(dev, 1);
  188. if (!host)
  189. return -ENOMEM;
  190. ap = host->ports[0];
  191. /* Setup ATA bindings */
  192. ap->ops = &at32_port_ops;
  193. ap->pio_mask = PIO_MASK;
  194. ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_SLAVE_POSS;
  195. /*
  196. * Since all 8-bit taskfile transfers has to go on the lower
  197. * byte of the data bus and there is a bug in the SMC that
  198. * makes it impossible to alter the bus width during runtime,
  199. * we need to hardwire the address signals as follows:
  200. *
  201. * A_IDE(2:0) <= A_EBI(3:1)
  202. *
  203. * This makes all addresses on the EBI even, thus all data
  204. * will be on the lower byte of the data bus. All addresses
  205. * used by libATA need to be altered according to this.
  206. */
  207. ap->ioaddr.altstatus_addr = info->alt_addr + (0x06 << 1);
  208. ap->ioaddr.ctl_addr = info->alt_addr + (0x06 << 1);
  209. ap->ioaddr.data_addr = info->ide_addr + (ATA_REG_DATA << 1);
  210. ap->ioaddr.error_addr = info->ide_addr + (ATA_REG_ERR << 1);
  211. ap->ioaddr.feature_addr = info->ide_addr + (ATA_REG_FEATURE << 1);
  212. ap->ioaddr.nsect_addr = info->ide_addr + (ATA_REG_NSECT << 1);
  213. ap->ioaddr.lbal_addr = info->ide_addr + (ATA_REG_LBAL << 1);
  214. ap->ioaddr.lbam_addr = info->ide_addr + (ATA_REG_LBAM << 1);
  215. ap->ioaddr.lbah_addr = info->ide_addr + (ATA_REG_LBAH << 1);
  216. ap->ioaddr.device_addr = info->ide_addr + (ATA_REG_DEVICE << 1);
  217. ap->ioaddr.status_addr = info->ide_addr + (ATA_REG_STATUS << 1);
  218. ap->ioaddr.command_addr = info->ide_addr + (ATA_REG_CMD << 1);
  219. /* Set info as private data of ATA host */
  220. host->private_data = info;
  221. /* Register ATA device and return */
  222. return ata_host_activate(host, info->irq, ata_interrupt,
  223. IRQF_SHARED | IRQF_TRIGGER_RISING,
  224. &at32_sht);
  225. }
  226. /*
  227. * This function may come in handy for people analyzing their own
  228. * EBI -> PATA adaptors.
  229. */
  230. #ifdef DEBUG_BUS
  231. static void __init pata_at32_debug_bus(struct device *dev,
  232. struct at32_ide_info *info)
  233. {
  234. const int d1 = 0xff;
  235. const int d2 = 0x00;
  236. int i;
  237. /* Write 8-bit values (registers) */
  238. iowrite8(d1, info->alt_addr + (0x06 << 1));
  239. iowrite8(d2, info->alt_addr + (0x06 << 1));
  240. for (i = 0; i < 8; i++) {
  241. iowrite8(d1, info->ide_addr + (i << 1));
  242. iowrite8(d2, info->ide_addr + (i << 1));
  243. }
  244. /* Write 16 bit values (data) */
  245. iowrite16(d1, info->ide_addr);
  246. iowrite16(d1 << 8, info->ide_addr);
  247. iowrite16(d1, info->ide_addr);
  248. iowrite16(d1 << 8, info->ide_addr);
  249. }
  250. #endif
  251. static int __init pata_at32_probe(struct platform_device *pdev)
  252. {
  253. const struct ata_timing initial_timing =
  254. {XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0};
  255. struct device *dev = &pdev->dev;
  256. struct at32_ide_info *info;
  257. struct ide_platform_data *board = pdev->dev.platform_data;
  258. struct resource *res;
  259. int irq;
  260. int ret;
  261. if (!board)
  262. return -ENXIO;
  263. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  264. if (!res)
  265. return -ENXIO;
  266. /* Retrive IRQ */
  267. irq = platform_get_irq(pdev, 0);
  268. if (irq < 0)
  269. return irq;
  270. /* Setup struct containing private infomation */
  271. info = kzalloc(sizeof(struct at32_ide_info), GFP_KERNEL);
  272. if (!info)
  273. return -ENOMEM;
  274. memset(info, 0, sizeof(struct at32_ide_info));
  275. info->irq = irq;
  276. info->cs = board->cs;
  277. /* Request memory resources */
  278. info->res_ide.start = res->start + CF_IDE_OFFSET;
  279. info->res_ide.end = info->res_ide.start + CF_RES_SIZE - 1;
  280. info->res_ide.name = "ide";
  281. info->res_ide.flags = IORESOURCE_MEM;
  282. ret = request_resource(res, &info->res_ide);
  283. if (ret)
  284. goto err_req_res_ide;
  285. info->res_alt.start = res->start + CF_ALT_IDE_OFFSET;
  286. info->res_alt.end = info->res_alt.start + CF_RES_SIZE - 1;
  287. info->res_alt.name = "alt";
  288. info->res_alt.flags = IORESOURCE_MEM;
  289. ret = request_resource(res, &info->res_alt);
  290. if (ret)
  291. goto err_req_res_alt;
  292. /* Setup non-timing elements of SMC */
  293. info->smc.bus_width = 2; /* 16 bit data bus */
  294. info->smc.nrd_controlled = 1; /* Sample data on rising edge of NRD */
  295. info->smc.nwe_controlled = 0; /* Drive data on falling edge of NCS */
  296. info->smc.nwait_mode = 3; /* NWAIT is in READY mode */
  297. info->smc.byte_write = 0; /* Byte select access type */
  298. info->smc.tdf_mode = 0; /* TDF optimization disabled */
  299. info->smc.tdf_cycles = 0; /* No TDF wait cycles */
  300. /* Setup SMC to ATA timing */
  301. ret = pata_at32_setup_timing(dev, info, &initial_timing);
  302. if (ret)
  303. goto err_setup_timing;
  304. /* Map ATA address space */
  305. ret = -ENOMEM;
  306. info->ide_addr = devm_ioremap(dev, info->res_ide.start, 16);
  307. info->alt_addr = devm_ioremap(dev, info->res_alt.start, 16);
  308. if (!info->ide_addr || !info->alt_addr)
  309. goto err_ioremap;
  310. #ifdef DEBUG_BUS
  311. pata_at32_debug_bus(dev, info);
  312. #endif
  313. /* Setup and register ATA device */
  314. ret = pata_at32_init_one(dev, info);
  315. if (ret)
  316. goto err_ata_device;
  317. return 0;
  318. err_ata_device:
  319. err_ioremap:
  320. err_setup_timing:
  321. release_resource(&info->res_alt);
  322. err_req_res_alt:
  323. release_resource(&info->res_ide);
  324. err_req_res_ide:
  325. kfree(info);
  326. return ret;
  327. }
  328. static int __exit pata_at32_remove(struct platform_device *pdev)
  329. {
  330. struct ata_host *host = platform_get_drvdata(pdev);
  331. struct at32_ide_info *info;
  332. if (!host)
  333. return 0;
  334. info = host->private_data;
  335. ata_host_detach(host);
  336. if (!info)
  337. return 0;
  338. release_resource(&info->res_ide);
  339. release_resource(&info->res_alt);
  340. kfree(info);
  341. return 0;
  342. }
  343. static struct platform_driver pata_at32_driver = {
  344. .remove = __exit_p(pata_at32_remove),
  345. .driver = {
  346. .name = "at32_ide",
  347. .owner = THIS_MODULE,
  348. },
  349. };
  350. static int __init pata_at32_init(void)
  351. {
  352. return platform_driver_probe(&pata_at32_driver, pata_at32_probe);
  353. }
  354. static void __exit pata_at32_exit(void)
  355. {
  356. platform_driver_unregister(&pata_at32_driver);
  357. }
  358. module_init(pata_at32_init);
  359. module_exit(pata_at32_exit);
  360. MODULE_LICENSE("GPL");
  361. MODULE_DESCRIPTION("AVR32 SMC/CFC PATA Driver");
  362. MODULE_AUTHOR("Kristoffer Nyborg Gregertsen <kngregertsen@norway.atmel.com>");
  363. MODULE_VERSION(DRV_VERSION);