pata_at91.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. * 2011 Igor Plyatov
  7. *
  8. * Based on:
  9. * * generic platform driver by Paul Mundt: drivers/ata/pata_platform.c
  10. * * pata_at32 driver by Kristoffer Nyborg Gregertsen
  11. * * at91_ide driver by Stanislaw Gruszka
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License version 2
  15. * as published by the Free Software Foundation.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/gfp.h>
  23. #include <scsi/scsi_host.h>
  24. #include <linux/ata.h>
  25. #include <linux/clk.h>
  26. #include <linux/libata.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/ata_platform.h>
  29. #include <mach/at91sam9_smc.h>
  30. #include <mach/board.h>
  31. #include <asm/gpio.h>
  32. #define DRV_NAME "pata_at91"
  33. #define DRV_VERSION "0.3"
  34. #define CF_IDE_OFFSET 0x00c00000
  35. #define CF_ALT_IDE_OFFSET 0x00e00000
  36. #define CF_IDE_RES_SIZE 0x08
  37. #define CS_PULSE_MAXIMUM 319
  38. #define ER_SMC_CALC 1
  39. #define ER_SMC_RECALC 2
  40. struct at91_ide_info {
  41. unsigned long mode;
  42. unsigned int cs;
  43. struct clk *mck;
  44. void __iomem *ide_addr;
  45. void __iomem *alt_addr;
  46. };
  47. /**
  48. * struct smc_range - range of valid values for SMC register.
  49. */
  50. struct smc_range {
  51. int min;
  52. int max;
  53. };
  54. /**
  55. * adjust_smc_value - adjust value for one of SMC registers.
  56. * @value: adjusted value
  57. * @range: array of SMC ranges with valid values
  58. * @size: SMC ranges array size
  59. *
  60. * This returns the difference between input and output value or negative
  61. * in case of invalid input value.
  62. * If negative returned, then output value = maximal possible from ranges.
  63. */
  64. static int adjust_smc_value(int *value, struct smc_range *range, int size)
  65. {
  66. int maximum = (range + size - 1)->max;
  67. int remainder;
  68. do {
  69. if (*value < range->min) {
  70. remainder = range->min - *value;
  71. *value = range->min; /* nearest valid value */
  72. return remainder;
  73. } else if ((range->min <= *value) && (*value <= range->max))
  74. return 0;
  75. range++;
  76. } while (--size);
  77. *value = maximum;
  78. return -1; /* invalid value */
  79. }
  80. /**
  81. * calc_smc_vals - calculate SMC register values
  82. * @dev: ATA device
  83. * @setup: SMC_SETUP register value
  84. * @pulse: SMC_PULSE register value
  85. * @cycle: SMC_CYCLE register value
  86. *
  87. * This returns negative in case of invalid values for SMC registers:
  88. * -ER_SMC_RECALC - recalculation required for SMC values,
  89. * -ER_SMC_CALC - calculation failed (invalid input values).
  90. *
  91. * SMC use special coding scheme, see "Coding and Range of Timing
  92. * Parameters" table from AT91SAM9 datasheets.
  93. *
  94. * SMC_SETUP = 128*setup[5] + setup[4:0]
  95. * SMC_PULSE = 256*pulse[6] + pulse[5:0]
  96. * SMC_CYCLE = 256*cycle[8:7] + cycle[6:0]
  97. */
  98. static int calc_smc_vals(struct device *dev,
  99. int *setup, int *pulse, int *cycle, int *cs_pulse)
  100. {
  101. int ret_val;
  102. int err = 0;
  103. struct smc_range range_setup[] = { /* SMC_SETUP valid values */
  104. {.min = 0, .max = 31}, /* first range */
  105. {.min = 128, .max = 159} /* second range */
  106. };
  107. struct smc_range range_pulse[] = { /* SMC_PULSE valid values */
  108. {.min = 0, .max = 63}, /* first range */
  109. {.min = 256, .max = 319} /* second range */
  110. };
  111. struct smc_range range_cycle[] = { /* SMC_CYCLE valid values */
  112. {.min = 0, .max = 127}, /* first range */
  113. {.min = 256, .max = 383}, /* second range */
  114. {.min = 512, .max = 639}, /* third range */
  115. {.min = 768, .max = 895} /* fourth range */
  116. };
  117. ret_val = adjust_smc_value(setup, range_setup, ARRAY_SIZE(range_setup));
  118. if (ret_val < 0)
  119. dev_warn(dev, "maximal SMC Setup value\n");
  120. else
  121. *cycle += ret_val;
  122. ret_val = adjust_smc_value(pulse, range_pulse, ARRAY_SIZE(range_pulse));
  123. if (ret_val < 0)
  124. dev_warn(dev, "maximal SMC Pulse value\n");
  125. else
  126. *cycle += ret_val;
  127. ret_val = adjust_smc_value(cycle, range_cycle, ARRAY_SIZE(range_cycle));
  128. if (ret_val < 0)
  129. dev_warn(dev, "maximal SMC Cycle value\n");
  130. *cs_pulse = *cycle;
  131. if (*cs_pulse > CS_PULSE_MAXIMUM) {
  132. dev_err(dev, "unable to calculate valid SMC settings\n");
  133. return -ER_SMC_CALC;
  134. }
  135. ret_val = adjust_smc_value(cs_pulse, range_pulse,
  136. ARRAY_SIZE(range_pulse));
  137. if (ret_val < 0) {
  138. dev_warn(dev, "maximal SMC CS Pulse value\n");
  139. } else if (ret_val != 0) {
  140. *cycle = *cs_pulse;
  141. dev_warn(dev, "SMC Cycle extended\n");
  142. err = -ER_SMC_RECALC;
  143. }
  144. return err;
  145. }
  146. /**
  147. * to_smc_format - convert values into SMC format
  148. * @setup: SETUP value of SMC Setup Register
  149. * @pulse: PULSE value of SMC Pulse Register
  150. * @cycle: CYCLE value of SMC Cycle Register
  151. * @cs_pulse: NCS_PULSE value of SMC Pulse Register
  152. */
  153. static void to_smc_format(int *setup, int *pulse, int *cycle, int *cs_pulse)
  154. {
  155. *setup = (*setup & 0x1f) | ((*setup & 0x80) >> 2);
  156. *pulse = (*pulse & 0x3f) | ((*pulse & 0x100) >> 2);
  157. *cycle = (*cycle & 0x7f) | ((*cycle & 0x300) >> 1);
  158. *cs_pulse = (*cs_pulse & 0x3f) | ((*cs_pulse & 0x100) >> 2);
  159. }
  160. static unsigned long calc_mck_cycles(unsigned long ns, unsigned long mck_hz)
  161. {
  162. unsigned long mul;
  163. /*
  164. * cycles = x [nsec] * f [Hz] / 10^9 [ns in sec] =
  165. * x * (f / 1_000_000_000) =
  166. * x * ((f * 65536) / 1_000_000_000) / 65536 =
  167. * x * (((f / 10_000) * 65536) / 100_000) / 65536 =
  168. */
  169. mul = (mck_hz / 10000) << 16;
  170. mul /= 100000;
  171. return (ns * mul + 65536) >> 16; /* rounding */
  172. }
  173. /**
  174. * set_smc_timing - SMC timings setup.
  175. * @dev: device
  176. * @info: AT91 IDE info
  177. * @ata: ATA timings
  178. *
  179. * Its assumed that write timings are same as read timings,
  180. * cs_setup = 0 and cs_pulse = cycle.
  181. */
  182. static void set_smc_timing(struct device *dev, struct ata_device *adev,
  183. struct at91_ide_info *info, const struct ata_timing *ata)
  184. {
  185. int ret = 0;
  186. int use_iordy;
  187. struct sam9_smc_config smc;
  188. unsigned int t6z; /* data tristate time in ns */
  189. unsigned int cycle; /* SMC Cycle width in MCK ticks */
  190. unsigned int setup; /* SMC Setup width in MCK ticks */
  191. unsigned int pulse; /* CFIOR and CFIOW pulse width in MCK ticks */
  192. unsigned int cs_pulse; /* CS4 or CS5 pulse width in MCK ticks*/
  193. unsigned int tdf_cycles; /* SMC TDF MCK ticks */
  194. unsigned long mck_hz; /* MCK frequency in Hz */
  195. t6z = (ata->mode < XFER_PIO_5) ? 30 : 20;
  196. mck_hz = clk_get_rate(info->mck);
  197. cycle = calc_mck_cycles(ata->cyc8b, mck_hz);
  198. setup = calc_mck_cycles(ata->setup, mck_hz);
  199. pulse = calc_mck_cycles(ata->act8b, mck_hz);
  200. tdf_cycles = calc_mck_cycles(t6z, mck_hz);
  201. do {
  202. ret = calc_smc_vals(dev, &setup, &pulse, &cycle, &cs_pulse);
  203. } while (ret == -ER_SMC_RECALC);
  204. if (ret == -ER_SMC_CALC)
  205. dev_err(dev, "Interface may not operate correctly\n");
  206. dev_dbg(dev, "SMC Setup=%u, Pulse=%u, Cycle=%u, CS Pulse=%u\n",
  207. setup, pulse, cycle, cs_pulse);
  208. to_smc_format(&setup, &pulse, &cycle, &cs_pulse);
  209. /* disable or enable waiting for IORDY signal */
  210. use_iordy = ata_pio_need_iordy(adev);
  211. if (use_iordy)
  212. info->mode |= AT91_SMC_EXNWMODE_READY;
  213. if (tdf_cycles > 15) {
  214. tdf_cycles = 15;
  215. dev_warn(dev, "maximal SMC TDF Cycles value\n");
  216. }
  217. dev_dbg(dev, "Use IORDY=%u, TDF Cycles=%u\n", use_iordy, tdf_cycles);
  218. /* SMC Setup Register */
  219. smc.nwe_setup = smc.nrd_setup = setup;
  220. smc.ncs_write_setup = smc.ncs_read_setup = 0;
  221. /* SMC Pulse Register */
  222. smc.nwe_pulse = smc.nrd_pulse = pulse;
  223. smc.ncs_write_pulse = smc.ncs_read_pulse = cs_pulse;
  224. /* SMC Cycle Register */
  225. smc.write_cycle = smc.read_cycle = cycle;
  226. /* SMC Mode Register*/
  227. smc.tdf_cycles = tdf_cycles;
  228. smc.mode = info->mode;
  229. sam9_smc_configure(0, info->cs, &smc);
  230. }
  231. static void pata_at91_set_piomode(struct ata_port *ap, struct ata_device *adev)
  232. {
  233. struct at91_ide_info *info = ap->host->private_data;
  234. struct ata_timing timing;
  235. int ret;
  236. /* Compute ATA timing and set it to SMC */
  237. ret = ata_timing_compute(adev, adev->pio_mode, &timing, 1000, 0);
  238. if (ret) {
  239. dev_warn(ap->dev, "Failed to compute ATA timing %d, "
  240. "set PIO_0 timing\n", ret);
  241. timing = *ata_timing_find_mode(XFER_PIO_0);
  242. }
  243. set_smc_timing(ap->dev, adev, info, &timing);
  244. }
  245. static unsigned int pata_at91_data_xfer_noirq(struct ata_device *dev,
  246. unsigned char *buf, unsigned int buflen, int rw)
  247. {
  248. struct at91_ide_info *info = dev->link->ap->host->private_data;
  249. unsigned int consumed;
  250. unsigned long flags;
  251. struct sam9_smc_config smc;
  252. local_irq_save(flags);
  253. sam9_smc_read_mode(0, info->cs, &smc);
  254. /* set 16bit mode before writing data */
  255. smc.mode = (smc.mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_16;
  256. sam9_smc_write_mode(0, info->cs, &smc);
  257. consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
  258. /* restore 8bit mode after data is written */
  259. smc.mode = (smc.mode & ~AT91_SMC_DBW) | AT91_SMC_DBW_8;
  260. sam9_smc_write_mode(0, info->cs, &smc);
  261. local_irq_restore(flags);
  262. return consumed;
  263. }
  264. static struct scsi_host_template pata_at91_sht = {
  265. ATA_PIO_SHT(DRV_NAME),
  266. };
  267. static struct ata_port_operations pata_at91_port_ops = {
  268. .inherits = &ata_sff_port_ops,
  269. .sff_data_xfer = pata_at91_data_xfer_noirq,
  270. .set_piomode = pata_at91_set_piomode,
  271. .cable_detect = ata_cable_40wire,
  272. };
  273. static int __devinit pata_at91_probe(struct platform_device *pdev)
  274. {
  275. struct at91_cf_data *board = pdev->dev.platform_data;
  276. struct device *dev = &pdev->dev;
  277. struct at91_ide_info *info;
  278. struct resource *mem_res;
  279. struct ata_host *host;
  280. struct ata_port *ap;
  281. int irq_flags = 0;
  282. int irq = 0;
  283. int ret;
  284. /* get platform resources: IO/CTL memories and irq/rst pins */
  285. if (pdev->num_resources != 1) {
  286. dev_err(&pdev->dev, "invalid number of resources\n");
  287. return -EINVAL;
  288. }
  289. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  290. if (!mem_res) {
  291. dev_err(dev, "failed to get mem resource\n");
  292. return -EINVAL;
  293. }
  294. irq = board->irq_pin;
  295. /* init ata host */
  296. host = ata_host_alloc(dev, 1);
  297. if (!host)
  298. return -ENOMEM;
  299. ap = host->ports[0];
  300. ap->ops = &pata_at91_port_ops;
  301. ap->flags |= ATA_FLAG_SLAVE_POSS;
  302. ap->pio_mask = ATA_PIO4;
  303. if (!gpio_is_valid(irq)) {
  304. ap->flags |= ATA_FLAG_PIO_POLLING;
  305. ata_port_desc(ap, "no IRQ, using PIO polling");
  306. }
  307. info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
  308. if (!info) {
  309. dev_err(dev, "failed to allocate memory for private data\n");
  310. return -ENOMEM;
  311. }
  312. info->mck = clk_get(NULL, "mck");
  313. if (IS_ERR(info->mck)) {
  314. dev_err(dev, "failed to get access to mck clock\n");
  315. return -ENODEV;
  316. }
  317. info->cs = board->chipselect;
  318. info->mode = AT91_SMC_READMODE | AT91_SMC_WRITEMODE |
  319. AT91_SMC_EXNWMODE_READY | AT91_SMC_BAT_SELECT |
  320. AT91_SMC_DBW_8 | AT91_SMC_TDF_(0);
  321. info->ide_addr = devm_ioremap(dev,
  322. mem_res->start + CF_IDE_OFFSET, CF_IDE_RES_SIZE);
  323. if (!info->ide_addr) {
  324. dev_err(dev, "failed to map IO base\n");
  325. ret = -ENOMEM;
  326. goto err_put;
  327. }
  328. info->alt_addr = devm_ioremap(dev,
  329. mem_res->start + CF_ALT_IDE_OFFSET, CF_IDE_RES_SIZE);
  330. if (!info->alt_addr) {
  331. dev_err(dev, "failed to map CTL base\n");
  332. ret = -ENOMEM;
  333. goto err_put;
  334. }
  335. ap->ioaddr.cmd_addr = info->ide_addr;
  336. ap->ioaddr.ctl_addr = info->alt_addr + 0x06;
  337. ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
  338. ata_sff_std_ports(&ap->ioaddr);
  339. ata_port_desc(ap, "mmio cmd 0x%llx ctl 0x%llx",
  340. (unsigned long long)mem_res->start + CF_IDE_OFFSET,
  341. (unsigned long long)mem_res->start + CF_ALT_IDE_OFFSET);
  342. host->private_data = info;
  343. ret = ata_host_activate(host, gpio_is_valid(irq) ? gpio_to_irq(irq) : 0,
  344. gpio_is_valid(irq) ? ata_sff_interrupt : NULL,
  345. irq_flags, &pata_at91_sht);
  346. if (ret)
  347. goto err_put;
  348. return 0;
  349. err_put:
  350. clk_put(info->mck);
  351. return ret;
  352. }
  353. static int __devexit pata_at91_remove(struct platform_device *pdev)
  354. {
  355. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  356. struct at91_ide_info *info;
  357. if (!host)
  358. return 0;
  359. info = host->private_data;
  360. ata_host_detach(host);
  361. if (!info)
  362. return 0;
  363. clk_put(info->mck);
  364. return 0;
  365. }
  366. static struct platform_driver pata_at91_driver = {
  367. .probe = pata_at91_probe,
  368. .remove = __devexit_p(pata_at91_remove),
  369. .driver = {
  370. .name = DRV_NAME,
  371. .owner = THIS_MODULE,
  372. },
  373. };
  374. module_platform_driver(pata_at91_driver);
  375. MODULE_LICENSE("GPL");
  376. MODULE_DESCRIPTION("Driver for CF in True IDE mode on AT91SAM9260 SoC");
  377. MODULE_AUTHOR("Matyukevich Sergey");
  378. MODULE_VERSION(DRV_VERSION);