pata_at32.c 11 KB

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