PageRenderTime 33ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/ide/palm_bk3710.c

https://github.com/mstsirkin/linux
C | 400 lines | 251 code | 66 blank | 83 comment | 15 complexity | 082756a15bf2be2975041358f5f75ce2 MD5 | raw file
  1. /*
  2. * Palmchip bk3710 IDE controller
  3. *
  4. * Copyright (C) 2006 Texas Instruments.
  5. * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. * ----------------------------------------------------------------------------
  23. *
  24. */
  25. #include <linux/types.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/ioport.h>
  29. #include <linux/ide.h>
  30. #include <linux/delay.h>
  31. #include <linux/init.h>
  32. #include <linux/clk.h>
  33. #include <linux/platform_device.h>
  34. /* Offset of the primary interface registers */
  35. #define IDE_PALM_ATA_PRI_REG_OFFSET 0x1F0
  36. /* Primary Control Offset */
  37. #define IDE_PALM_ATA_PRI_CTL_OFFSET 0x3F6
  38. #define BK3710_BMICP 0x00
  39. #define BK3710_BMISP 0x02
  40. #define BK3710_BMIDTP 0x04
  41. #define BK3710_IDETIMP 0x40
  42. #define BK3710_IDESTATUS 0x47
  43. #define BK3710_UDMACTL 0x48
  44. #define BK3710_MISCCTL 0x50
  45. #define BK3710_REGSTB 0x54
  46. #define BK3710_REGRCVR 0x58
  47. #define BK3710_DATSTB 0x5C
  48. #define BK3710_DATRCVR 0x60
  49. #define BK3710_DMASTB 0x64
  50. #define BK3710_DMARCVR 0x68
  51. #define BK3710_UDMASTB 0x6C
  52. #define BK3710_UDMATRP 0x70
  53. #define BK3710_UDMAENV 0x74
  54. #define BK3710_IORDYTMP 0x78
  55. static unsigned ideclk_period; /* in nanoseconds */
  56. struct palm_bk3710_udmatiming {
  57. unsigned int rptime; /* tRP -- Ready to pause time (nsec) */
  58. unsigned int cycletime; /* tCYCTYP2/2 -- avg Cycle Time (nsec) */
  59. /* tENV is always a minimum of 20 nsec */
  60. };
  61. static const struct palm_bk3710_udmatiming palm_bk3710_udmatimings[6] = {
  62. { 160, 240 / 2 }, /* UDMA Mode 0 */
  63. { 125, 160 / 2 }, /* UDMA Mode 1 */
  64. { 100, 120 / 2 }, /* UDMA Mode 2 */
  65. { 100, 90 / 2 }, /* UDMA Mode 3 */
  66. { 100, 60 / 2 }, /* UDMA Mode 4 */
  67. { 85, 40 / 2 }, /* UDMA Mode 5 */
  68. };
  69. static void palm_bk3710_setudmamode(void __iomem *base, unsigned int dev,
  70. unsigned int mode)
  71. {
  72. u8 tenv, trp, t0;
  73. u32 val32;
  74. u16 val16;
  75. /* DMA Data Setup */
  76. t0 = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].cycletime,
  77. ideclk_period) - 1;
  78. tenv = DIV_ROUND_UP(20, ideclk_period) - 1;
  79. trp = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].rptime,
  80. ideclk_period) - 1;
  81. /* udmastb Ultra DMA Access Strobe Width */
  82. val32 = readl(base + BK3710_UDMASTB) & (0xFF << (dev ? 0 : 8));
  83. val32 |= (t0 << (dev ? 8 : 0));
  84. writel(val32, base + BK3710_UDMASTB);
  85. /* udmatrp Ultra DMA Ready to Pause Time */
  86. val32 = readl(base + BK3710_UDMATRP) & (0xFF << (dev ? 0 : 8));
  87. val32 |= (trp << (dev ? 8 : 0));
  88. writel(val32, base + BK3710_UDMATRP);
  89. /* udmaenv Ultra DMA envelop Time */
  90. val32 = readl(base + BK3710_UDMAENV) & (0xFF << (dev ? 0 : 8));
  91. val32 |= (tenv << (dev ? 8 : 0));
  92. writel(val32, base + BK3710_UDMAENV);
  93. /* Enable UDMA for Device */
  94. val16 = readw(base + BK3710_UDMACTL) | (1 << dev);
  95. writew(val16, base + BK3710_UDMACTL);
  96. }
  97. static void palm_bk3710_setdmamode(void __iomem *base, unsigned int dev,
  98. unsigned short min_cycle,
  99. unsigned int mode)
  100. {
  101. u8 td, tkw, t0;
  102. u32 val32;
  103. u16 val16;
  104. struct ide_timing *t;
  105. int cycletime;
  106. t = ide_timing_find_mode(mode);
  107. cycletime = max_t(int, t->cycle, min_cycle);
  108. /* DMA Data Setup */
  109. t0 = DIV_ROUND_UP(cycletime, ideclk_period);
  110. td = DIV_ROUND_UP(t->active, ideclk_period);
  111. tkw = t0 - td - 1;
  112. td -= 1;
  113. val32 = readl(base + BK3710_DMASTB) & (0xFF << (dev ? 0 : 8));
  114. val32 |= (td << (dev ? 8 : 0));
  115. writel(val32, base + BK3710_DMASTB);
  116. val32 = readl(base + BK3710_DMARCVR) & (0xFF << (dev ? 0 : 8));
  117. val32 |= (tkw << (dev ? 8 : 0));
  118. writel(val32, base + BK3710_DMARCVR);
  119. /* Disable UDMA for Device */
  120. val16 = readw(base + BK3710_UDMACTL) & ~(1 << dev);
  121. writew(val16, base + BK3710_UDMACTL);
  122. }
  123. static void palm_bk3710_setpiomode(void __iomem *base, ide_drive_t *mate,
  124. unsigned int dev, unsigned int cycletime,
  125. unsigned int mode)
  126. {
  127. u8 t2, t2i, t0;
  128. u32 val32;
  129. struct ide_timing *t;
  130. t = ide_timing_find_mode(XFER_PIO_0 + mode);
  131. /* PIO Data Setup */
  132. t0 = DIV_ROUND_UP(cycletime, ideclk_period);
  133. t2 = DIV_ROUND_UP(t->active, ideclk_period);
  134. t2i = t0 - t2 - 1;
  135. t2 -= 1;
  136. val32 = readl(base + BK3710_DATSTB) & (0xFF << (dev ? 0 : 8));
  137. val32 |= (t2 << (dev ? 8 : 0));
  138. writel(val32, base + BK3710_DATSTB);
  139. val32 = readl(base + BK3710_DATRCVR) & (0xFF << (dev ? 0 : 8));
  140. val32 |= (t2i << (dev ? 8 : 0));
  141. writel(val32, base + BK3710_DATRCVR);
  142. if (mate) {
  143. u8 mode2 = mate->pio_mode - XFER_PIO_0;
  144. if (mode2 < mode)
  145. mode = mode2;
  146. }
  147. /* TASKFILE Setup */
  148. t0 = DIV_ROUND_UP(t->cyc8b, ideclk_period);
  149. t2 = DIV_ROUND_UP(t->act8b, ideclk_period);
  150. t2i = t0 - t2 - 1;
  151. t2 -= 1;
  152. val32 = readl(base + BK3710_REGSTB) & (0xFF << (dev ? 0 : 8));
  153. val32 |= (t2 << (dev ? 8 : 0));
  154. writel(val32, base + BK3710_REGSTB);
  155. val32 = readl(base + BK3710_REGRCVR) & (0xFF << (dev ? 0 : 8));
  156. val32 |= (t2i << (dev ? 8 : 0));
  157. writel(val32, base + BK3710_REGRCVR);
  158. }
  159. static void palm_bk3710_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  160. {
  161. int is_slave = drive->dn & 1;
  162. void __iomem *base = (void *)hwif->dma_base;
  163. const u8 xferspeed = drive->dma_mode;
  164. if (xferspeed >= XFER_UDMA_0) {
  165. palm_bk3710_setudmamode(base, is_slave,
  166. xferspeed - XFER_UDMA_0);
  167. } else {
  168. palm_bk3710_setdmamode(base, is_slave,
  169. drive->id[ATA_ID_EIDE_DMA_MIN],
  170. xferspeed);
  171. }
  172. }
  173. static void palm_bk3710_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  174. {
  175. unsigned int cycle_time;
  176. int is_slave = drive->dn & 1;
  177. ide_drive_t *mate;
  178. void __iomem *base = (void *)hwif->dma_base;
  179. const u8 pio = drive->pio_mode - XFER_PIO_0;
  180. /*
  181. * Obtain the drive PIO data for tuning the Palm Chip registers
  182. */
  183. cycle_time = ide_pio_cycle_time(drive, pio);
  184. mate = ide_get_pair_dev(drive);
  185. palm_bk3710_setpiomode(base, mate, is_slave, cycle_time, pio);
  186. }
  187. static void __devinit palm_bk3710_chipinit(void __iomem *base)
  188. {
  189. /*
  190. * REVISIT: the ATA reset signal needs to be managed through a
  191. * GPIO, which means it should come from platform_data. Until
  192. * we get and use such information, we have to trust that things
  193. * have been reset before we get here.
  194. */
  195. /*
  196. * Program the IDETIMP Register Value based on the following assumptions
  197. *
  198. * (ATA_IDETIMP_IDEEN , ENABLE ) |
  199. * (ATA_IDETIMP_PREPOST1 , DISABLE) |
  200. * (ATA_IDETIMP_PREPOST0 , DISABLE) |
  201. *
  202. * DM6446 silicon rev 2.1 and earlier have no observed net benefit
  203. * from enabling prefetch/postwrite.
  204. */
  205. writew(BIT(15), base + BK3710_IDETIMP);
  206. /*
  207. * UDMACTL Ultra-ATA DMA Control
  208. * (ATA_UDMACTL_UDMAP1 , 0 ) |
  209. * (ATA_UDMACTL_UDMAP0 , 0 )
  210. *
  211. */
  212. writew(0, base + BK3710_UDMACTL);
  213. /*
  214. * MISCCTL Miscellaneous Conrol Register
  215. * (ATA_MISCCTL_HWNHLD1P , 1 cycle)
  216. * (ATA_MISCCTL_HWNHLD0P , 1 cycle)
  217. * (ATA_MISCCTL_TIMORIDE , 1)
  218. */
  219. writel(0x001, base + BK3710_MISCCTL);
  220. /*
  221. * IORDYTMP IORDY Timer for Primary Register
  222. * (ATA_IORDYTMP_IORDYTMP , 0xffff )
  223. */
  224. writel(0xFFFF, base + BK3710_IORDYTMP);
  225. /*
  226. * Configure BMISP Register
  227. * (ATA_BMISP_DMAEN1 , DISABLE ) |
  228. * (ATA_BMISP_DMAEN0 , DISABLE ) |
  229. * (ATA_BMISP_IORDYINT , CLEAR) |
  230. * (ATA_BMISP_INTRSTAT , CLEAR) |
  231. * (ATA_BMISP_DMAERROR , CLEAR)
  232. */
  233. writew(0, base + BK3710_BMISP);
  234. palm_bk3710_setpiomode(base, NULL, 0, 600, 0);
  235. palm_bk3710_setpiomode(base, NULL, 1, 600, 0);
  236. }
  237. static u8 palm_bk3710_cable_detect(ide_hwif_t *hwif)
  238. {
  239. return ATA_CBL_PATA80;
  240. }
  241. static int __devinit palm_bk3710_init_dma(ide_hwif_t *hwif,
  242. const struct ide_port_info *d)
  243. {
  244. printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
  245. if (ide_allocate_dma_engine(hwif))
  246. return -1;
  247. hwif->dma_base = hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET;
  248. return 0;
  249. }
  250. static const struct ide_port_ops palm_bk3710_ports_ops = {
  251. .set_pio_mode = palm_bk3710_set_pio_mode,
  252. .set_dma_mode = palm_bk3710_set_dma_mode,
  253. .cable_detect = palm_bk3710_cable_detect,
  254. };
  255. static struct ide_port_info __devinitdata palm_bk3710_port_info = {
  256. .init_dma = palm_bk3710_init_dma,
  257. .port_ops = &palm_bk3710_ports_ops,
  258. .dma_ops = &sff_dma_ops,
  259. .host_flags = IDE_HFLAG_MMIO,
  260. .pio_mask = ATA_PIO4,
  261. .mwdma_mask = ATA_MWDMA2,
  262. .chipset = ide_palm3710,
  263. };
  264. static int __init palm_bk3710_probe(struct platform_device *pdev)
  265. {
  266. struct clk *clk;
  267. struct resource *mem, *irq;
  268. void __iomem *base;
  269. unsigned long rate, mem_size;
  270. int i, rc;
  271. struct ide_hw hw, *hws[] = { &hw };
  272. clk = clk_get(&pdev->dev, NULL);
  273. if (IS_ERR(clk))
  274. return -ENODEV;
  275. clk_enable(clk);
  276. rate = clk_get_rate(clk);
  277. /* NOTE: round *down* to meet minimum timings; we count in clocks */
  278. ideclk_period = 1000000000UL / rate;
  279. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  280. if (mem == NULL) {
  281. printk(KERN_ERR "failed to get memory region resource\n");
  282. return -ENODEV;
  283. }
  284. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  285. if (irq == NULL) {
  286. printk(KERN_ERR "failed to get IRQ resource\n");
  287. return -ENODEV;
  288. }
  289. mem_size = resource_size(mem);
  290. if (request_mem_region(mem->start, mem_size, "palm_bk3710") == NULL) {
  291. printk(KERN_ERR "failed to request memory region\n");
  292. return -EBUSY;
  293. }
  294. base = ioremap(mem->start, mem_size);
  295. if (!base) {
  296. printk(KERN_ERR "failed to map IO memory\n");
  297. release_mem_region(mem->start, mem_size);
  298. return -ENOMEM;
  299. }
  300. /* Configure the Palm Chip controller */
  301. palm_bk3710_chipinit(base);
  302. memset(&hw, 0, sizeof(hw));
  303. for (i = 0; i < IDE_NR_PORTS - 2; i++)
  304. hw.io_ports_array[i] = (unsigned long)
  305. (base + IDE_PALM_ATA_PRI_REG_OFFSET + i);
  306. hw.io_ports.ctl_addr = (unsigned long)
  307. (base + IDE_PALM_ATA_PRI_CTL_OFFSET);
  308. hw.irq = irq->start;
  309. hw.dev = &pdev->dev;
  310. palm_bk3710_port_info.udma_mask = rate < 100000000 ? ATA_UDMA4 :
  311. ATA_UDMA5;
  312. /* Register the IDE interface with Linux */
  313. rc = ide_host_add(&palm_bk3710_port_info, hws, 1, NULL);
  314. if (rc)
  315. goto out;
  316. return 0;
  317. out:
  318. printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n");
  319. return rc;
  320. }
  321. /* work with hotplug and coldplug */
  322. MODULE_ALIAS("platform:palm_bk3710");
  323. static struct platform_driver platform_bk_driver = {
  324. .driver = {
  325. .name = "palm_bk3710",
  326. .owner = THIS_MODULE,
  327. },
  328. };
  329. static int __init palm_bk3710_init(void)
  330. {
  331. return platform_driver_probe(&platform_bk_driver, palm_bk3710_probe);
  332. }
  333. module_init(palm_bk3710_init);
  334. MODULE_LICENSE("GPL");