pata_at91.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * PATA driver for AT91SAM9260 Static Memory Controller
  3. * with CompactFlash interface in True IDE mode
  4. *
  5. * Copyright (C) 2009 Matyukevich Sergey
  6. *
  7. * Based on:
  8. * * generic platform driver by Paul Mundt: drivers/ata/pata_platform.c
  9. * * pata_at32 driver by Kristoffer Nyborg Gregertsen
  10. * * at91_ide driver by Stanislaw Gruszka
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License version 2
  14. * as published by the Free Software Foundation.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/gfp.h>
  22. #include <scsi/scsi_host.h>
  23. #include <linux/ata.h>
  24. #include <linux/clk.h>
  25. #include <linux/libata.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/ata_platform.h>
  28. #include <mach/at91sam9_smc.h>
  29. #include <mach/board.h>
  30. #include <mach/gpio.h>
  31. #define DRV_NAME "pata_at91"
  32. #define DRV_VERSION "0.2"
  33. #define CF_IDE_OFFSET 0x00c00000
  34. #define CF_ALT_IDE_OFFSET 0x00e00000
  35. #define CF_IDE_RES_SIZE 0x08
  36. #define NCS_RD_PULSE_LIMIT 0x3f /* maximal value for pulse bitfields */
  37. struct at91_ide_info {
  38. unsigned long mode;
  39. unsigned int cs;
  40. struct clk *mck;
  41. void __iomem *ide_addr;
  42. void __iomem *alt_addr;
  43. };
  44. static const struct ata_timing initial_timing = {
  45. .mode = XFER_PIO_0,
  46. .setup = 70,
  47. .act8b = 290,
  48. .rec8b = 240,
  49. .cyc8b = 600,
  50. .active = 165,
  51. .recover = 150,
  52. .dmack_hold = 0,
  53. .cycle = 600,
  54. .udma = 0
  55. };
  56. static unsigned long calc_mck_cycles(unsigned long ns, unsigned long mck_hz)
  57. {
  58. unsigned long mul;
  59. /*
  60. * cycles = x [nsec] * f [Hz] / 10^9 [ns in sec] =
  61. * x * (f / 1_000_000_000) =
  62. * x * ((f * 65536) / 1_000_000_000) / 65536 =
  63. * x * (((f / 10_000) * 65536) / 100_000) / 65536 =
  64. */
  65. mul = (mck_hz / 10000) << 16;
  66. mul /= 100000;
  67. return (ns * mul + 65536) >> 16; /* rounding */
  68. }
  69. static void set_smc_mode(struct at91_ide_info *info)
  70. {
  71. at91_sys_write(AT91_SMC_MODE(info->cs), info->mode);
  72. return;
  73. }
  74. static void set_smc_timing(struct device *dev,
  75. struct at91_ide_info *info, const struct ata_timing *ata)
  76. {
  77. unsigned long read_cycle, write_cycle, active, recover;
  78. unsigned long nrd_setup, nrd_pulse, nrd_recover;
  79. unsigned long nwe_setup, nwe_pulse;
  80. unsigned long ncs_write_setup, ncs_write_pulse;
  81. unsigned long ncs_read_setup, ncs_read_pulse;
  82. unsigned long mck_hz;
  83. read_cycle = ata->cyc8b;
  84. nrd_setup = ata->setup;
  85. nrd_pulse = ata->act8b;
  86. nrd_recover = ata->rec8b;
  87. mck_hz = clk_get_rate(info->mck);
  88. read_cycle = calc_mck_cycles(read_cycle, mck_hz);
  89. nrd_setup = calc_mck_cycles(nrd_setup, mck_hz);
  90. nrd_pulse = calc_mck_cycles(nrd_pulse, mck_hz);
  91. nrd_recover = calc_mck_cycles(nrd_recover, mck_hz);
  92. active = nrd_setup + nrd_pulse;
  93. recover = read_cycle - active;
  94. /* Need at least two cycles recovery */
  95. if (recover < 2)
  96. read_cycle = active + 2;
  97. /* (CS0, CS1, DIR, OE) <= (CFCE1, CFCE2, CFRNW, NCSX) timings */
  98. ncs_read_setup = 1;
  99. ncs_read_pulse = read_cycle - 2;
  100. if (ncs_read_pulse > NCS_RD_PULSE_LIMIT) {
  101. ncs_read_pulse = NCS_RD_PULSE_LIMIT;
  102. dev_warn(dev, "ncs_read_pulse limited to maximal value %lu\n",
  103. ncs_read_pulse);
  104. }
  105. /* Write timings same as read timings */
  106. write_cycle = read_cycle;
  107. nwe_setup = nrd_setup;
  108. nwe_pulse = nrd_pulse;
  109. ncs_write_setup = ncs_read_setup;
  110. ncs_write_pulse = ncs_read_pulse;
  111. dev_dbg(dev, "ATA timings: nrd_setup = %lu nrd_pulse = %lu nrd_cycle = %lu\n",
  112. nrd_setup, nrd_pulse, read_cycle);
  113. dev_dbg(dev, "ATA timings: nwe_setup = %lu nwe_pulse = %lu nwe_cycle = %lu\n",
  114. nwe_setup, nwe_pulse, write_cycle);
  115. dev_dbg(dev, "ATA timings: ncs_read_setup = %lu ncs_read_pulse = %lu\n",
  116. ncs_read_setup, ncs_read_pulse);
  117. dev_dbg(dev, "ATA timings: ncs_write_setup = %lu ncs_write_pulse = %lu\n",
  118. ncs_write_setup, ncs_write_pulse);
  119. at91_sys_write(AT91_SMC_SETUP(info->cs),
  120. AT91_SMC_NWESETUP_(nwe_setup) |
  121. AT91_SMC_NRDSETUP_(nrd_setup) |
  122. AT91_SMC_NCS_WRSETUP_(ncs_write_setup) |
  123. AT91_SMC_NCS_RDSETUP_(ncs_read_setup));
  124. at91_sys_write(AT91_SMC_PULSE(info->cs),
  125. AT91_SMC_NWEPULSE_(nwe_pulse) |
  126. AT91_SMC_NRDPULSE_(nrd_pulse) |
  127. AT91_SMC_NCS_WRPULSE_(ncs_write_pulse) |
  128. AT91_SMC_NCS_RDPULSE_(ncs_read_pulse));
  129. at91_sys_write(AT91_SMC_CYCLE(info->cs),
  130. AT91_SMC_NWECYCLE_(write_cycle) |
  131. AT91_SMC_NRDCYCLE_(read_cycle));
  132. return;
  133. }
  134. static void pata_at91_set_piomode(struct ata_port *ap, struct ata_device *adev)
  135. {
  136. struct at91_ide_info *info = ap->host->private_data;
  137. struct ata_timing timing;
  138. int ret;
  139. /* Compute ATA timing and set it to SMC */
  140. ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
  141. if (ret) {
  142. dev_warn(ap->dev, "Failed to compute ATA timing %d, "
  143. "set PIO_0 timing\n", ret);
  144. set_smc_timing(ap->dev, info, &initial_timing);
  145. } else {
  146. set_smc_timing(ap->dev, info, &timing);
  147. }
  148. /* Setup SMC mode */
  149. set_smc_mode(info);
  150. return;
  151. }
  152. static unsigned int pata_at91_data_xfer_noirq(struct ata_device *dev,
  153. unsigned char *buf, unsigned int buflen, int rw)
  154. {
  155. struct at91_ide_info *info = dev->link->ap->host->private_data;
  156. unsigned int consumed;
  157. unsigned long flags;
  158. unsigned int mode;
  159. local_irq_save(flags);
  160. mode = at91_sys_read(AT91_SMC_MODE(info->cs));
  161. /* set 16bit mode before writing data */
  162. at91_sys_write(AT91_SMC_MODE(info->cs),
  163. (mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_16);
  164. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  165. /* restore 8bit mode after data is written */
  166. at91_sys_write(AT91_SMC_MODE(info->cs),
  167. (mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_8);
  168. local_irq_restore(flags);
  169. return consumed;
  170. }
  171. static struct scsi_host_template pata_at91_sht = {
  172. ATA_PIO_SHT(DRV_NAME),
  173. };
  174. static struct ata_port_operations pata_at91_port_ops = {
  175. .inherits = &ata_sff_port_ops,
  176. .sff_data_xfer = pata_at91_data_xfer_noirq,
  177. .set_piomode = pata_at91_set_piomode,
  178. .cable_detect = ata_cable_40wire,
  179. };
  180. static int __devinit pata_at91_probe(struct platform_device *pdev)
  181. {
  182. struct at91_cf_data *board = pdev->dev.platform_data;
  183. struct device *dev = &pdev->dev;
  184. struct at91_ide_info *info;
  185. struct resource *mem_res;
  186. struct ata_host *host;
  187. struct ata_port *ap;
  188. int irq_flags = 0;
  189. int irq = 0;
  190. int ret;
  191. /* get platform resources: IO/CTL memories and irq/rst pins */
  192. if (pdev->num_resources != 1) {
  193. dev_err(&pdev->dev, "invalid number of resources\n");
  194. return -EINVAL;
  195. }
  196. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  197. if (!mem_res) {
  198. dev_err(dev, "failed to get mem resource\n");
  199. return -EINVAL;
  200. }
  201. irq = board->irq_pin;
  202. /* init ata host */
  203. host = ata_host_alloc(dev, 1);
  204. if (!host)
  205. return -ENOMEM;
  206. ap = host->ports[0];
  207. ap->ops = &pata_at91_port_ops;
  208. ap->flags |= ATA_FLAG_SLAVE_POSS;
  209. ap->pio_mask = ATA_PIO4;
  210. if (!irq) {
  211. ap->flags |= ATA_FLAG_PIO_POLLING;
  212. ata_port_desc(ap, "no IRQ, using PIO polling");
  213. }
  214. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  215. if (!info) {
  216. dev_err(dev, "failed to allocate memory for private data\n");
  217. return -ENOMEM;
  218. }
  219. info->mck = clk_get(NULL, "mck");
  220. if (IS_ERR(info->mck)) {
  221. dev_err(dev, "failed to get access to mck clock\n");
  222. return -ENODEV;
  223. }
  224. info->cs = board->chipselect;
  225. info->mode = AT91_SMC_READMODE | AT91_SMC_WRITEMODE |
  226. AT91_SMC_EXNWMODE_READY | AT91_SMC_BAT_SELECT |
  227. AT91_SMC_DBW_8 | AT91_SMC_TDF_(0);
  228. info->ide_addr = devm_ioremap(dev,
  229. mem_res->start + CF_IDE_OFFSET, CF_IDE_RES_SIZE);
  230. if (!info->ide_addr) {
  231. dev_err(dev, "failed to map IO base\n");
  232. ret = -ENOMEM;
  233. goto err_put;
  234. }
  235. info->alt_addr = devm_ioremap(dev,
  236. mem_res->start + CF_ALT_IDE_OFFSET, CF_IDE_RES_SIZE);
  237. if (!info->alt_addr) {
  238. dev_err(dev, "failed to map CTL base\n");
  239. ret = -ENOMEM;
  240. goto err_put;
  241. }
  242. ap->ioaddr.cmd_addr = info->ide_addr;
  243. ap->ioaddr.ctl_addr = info->alt_addr + 0x06;
  244. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  245. ata_sff_std_ports(&ap->ioaddr);
  246. ata_port_desc(ap, "mmio cmd 0x%llx ctl 0x%llx",
  247. (unsigned long long)mem_res->start + CF_IDE_OFFSET,
  248. (unsigned long long)mem_res->start + CF_ALT_IDE_OFFSET);
  249. host->private_data = info;
  250. return ata_host_activate(host, irq ? gpio_to_irq(irq) : 0,
  251. irq ? ata_sff_interrupt : NULL,
  252. irq_flags, &pata_at91_sht);
  253. err_put:
  254. clk_put(info->mck);
  255. return ret;
  256. }
  257. static int __devexit pata_at91_remove(struct platform_device *pdev)
  258. {
  259. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  260. struct at91_ide_info *info;
  261. if (!host)
  262. return 0;
  263. info = host->private_data;
  264. ata_host_detach(host);
  265. if (!info)
  266. return 0;
  267. clk_put(info->mck);
  268. return 0;
  269. }
  270. static struct platform_driver pata_at91_driver = {
  271. .probe = pata_at91_probe,
  272. .remove = __devexit_p(pata_at91_remove),
  273. .driver = {
  274. .name = DRV_NAME,
  275. .owner = THIS_MODULE,
  276. },
  277. };
  278. static int __init pata_at91_init(void)
  279. {
  280. return platform_driver_register(&pata_at91_driver);
  281. }
  282. static void __exit pata_at91_exit(void)
  283. {
  284. platform_driver_unregister(&pata_at91_driver);
  285. }
  286. module_init(pata_at91_init);
  287. module_exit(pata_at91_exit);
  288. MODULE_LICENSE("GPL");
  289. MODULE_DESCRIPTION("Driver for CF in True IDE mode on AT91SAM9260 SoC");
  290. MODULE_AUTHOR("Matyukevich Sergey");
  291. MODULE_VERSION(DRV_VERSION);