pata_atp867x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pata_atp867x.c - ARTOP 867X 64bit 4-channel UDMA133 ATA controller driver
  4. *
  5. * (C) 2009 Google Inc. John(Jung-Ik) Lee <jilee@google.com>
  6. *
  7. * Per Atp867 data sheet rev 1.2, Acard.
  8. * Based in part on early ide code from
  9. * 2003-2004 by Eric Uhrhane, Google, Inc.
  10. *
  11. * TODO:
  12. * 1. RAID features [comparison, XOR, striping, mirroring, etc.]
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/pci.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/gfp.h>
  21. #include <scsi/scsi_host.h>
  22. #include <linux/libata.h>
  23. #define DRV_NAME "pata_atp867x"
  24. #define DRV_VERSION "0.7.5"
  25. /*
  26. * IO Registers
  27. * Note that all runtime hot priv ports are cached in ap private_data
  28. */
  29. enum {
  30. ATP867X_IO_CHANNEL_OFFSET = 0x10,
  31. /*
  32. * IO Register Bitfields
  33. */
  34. ATP867X_IO_PIOSPD_ACTIVE_SHIFT = 4,
  35. ATP867X_IO_PIOSPD_RECOVER_SHIFT = 0,
  36. ATP867X_IO_DMAMODE_MSTR_SHIFT = 0,
  37. ATP867X_IO_DMAMODE_MSTR_MASK = 0x07,
  38. ATP867X_IO_DMAMODE_SLAVE_SHIFT = 4,
  39. ATP867X_IO_DMAMODE_SLAVE_MASK = 0x70,
  40. ATP867X_IO_DMAMODE_UDMA_6 = 0x07,
  41. ATP867X_IO_DMAMODE_UDMA_5 = 0x06,
  42. ATP867X_IO_DMAMODE_UDMA_4 = 0x05,
  43. ATP867X_IO_DMAMODE_UDMA_3 = 0x04,
  44. ATP867X_IO_DMAMODE_UDMA_2 = 0x03,
  45. ATP867X_IO_DMAMODE_UDMA_1 = 0x02,
  46. ATP867X_IO_DMAMODE_UDMA_0 = 0x01,
  47. ATP867X_IO_DMAMODE_DISABLE = 0x00,
  48. ATP867X_IO_SYS_INFO_66MHZ = 0x04,
  49. ATP867X_IO_SYS_INFO_SLOW_UDMA5 = 0x02,
  50. ATP867X_IO_SYS_MASK_RESERVED = (~0xf1),
  51. ATP867X_IO_PORTSPD_VAL = 0x1143,
  52. ATP867X_PREREAD_VAL = 0x0200,
  53. ATP867X_NUM_PORTS = 4,
  54. ATP867X_BAR_IOBASE = 0,
  55. ATP867X_BAR_ROMBASE = 6,
  56. };
  57. #define ATP867X_IOBASE(ap) ((ap)->host->iomap[0])
  58. #define ATP867X_SYS_INFO(ap) (0x3F + ATP867X_IOBASE(ap))
  59. #define ATP867X_IO_PORTBASE(ap, port) (0x00 + ATP867X_IOBASE(ap) + \
  60. (port) * ATP867X_IO_CHANNEL_OFFSET)
  61. #define ATP867X_IO_DMABASE(ap, port) (0x40 + \
  62. ATP867X_IO_PORTBASE((ap), (port)))
  63. #define ATP867X_IO_STATUS(ap, port) (0x07 + \
  64. ATP867X_IO_PORTBASE((ap), (port)))
  65. #define ATP867X_IO_ALTSTATUS(ap, port) (0x0E + \
  66. ATP867X_IO_PORTBASE((ap), (port)))
  67. /*
  68. * hot priv ports
  69. */
  70. #define ATP867X_IO_MSTRPIOSPD(ap, port) (0x08 + \
  71. ATP867X_IO_DMABASE((ap), (port)))
  72. #define ATP867X_IO_SLAVPIOSPD(ap, port) (0x09 + \
  73. ATP867X_IO_DMABASE((ap), (port)))
  74. #define ATP867X_IO_8BPIOSPD(ap, port) (0x0A + \
  75. ATP867X_IO_DMABASE((ap), (port)))
  76. #define ATP867X_IO_DMAMODE(ap, port) (0x0B + \
  77. ATP867X_IO_DMABASE((ap), (port)))
  78. #define ATP867X_IO_PORTSPD(ap, port) (0x4A + \
  79. ATP867X_IO_PORTBASE((ap), (port)))
  80. #define ATP867X_IO_PREREAD(ap, port) (0x4C + \
  81. ATP867X_IO_PORTBASE((ap), (port)))
  82. struct atp867x_priv {
  83. void __iomem *dma_mode;
  84. void __iomem *mstr_piospd;
  85. void __iomem *slave_piospd;
  86. void __iomem *eightb_piospd;
  87. int pci66mhz;
  88. };
  89. static void atp867x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  90. {
  91. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  92. struct atp867x_priv *dp = ap->private_data;
  93. u8 speed = adev->dma_mode;
  94. u8 b;
  95. u8 mode = speed - XFER_UDMA_0 + 1;
  96. /*
  97. * Doc 6.6.9: decrease the udma mode value by 1 for safer UDMA speed
  98. * on 66MHz bus
  99. * rev-A: UDMA_1~4 (5, 6 no change)
  100. * rev-B: all UDMA modes
  101. * UDMA_0 stays not to disable UDMA
  102. */
  103. if (dp->pci66mhz && mode > ATP867X_IO_DMAMODE_UDMA_0 &&
  104. (pdev->device == PCI_DEVICE_ID_ARTOP_ATP867B ||
  105. mode < ATP867X_IO_DMAMODE_UDMA_5))
  106. mode--;
  107. b = ioread8(dp->dma_mode);
  108. if (adev->devno & 1) {
  109. b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK) |
  110. (mode << ATP867X_IO_DMAMODE_SLAVE_SHIFT);
  111. } else {
  112. b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK) |
  113. (mode << ATP867X_IO_DMAMODE_MSTR_SHIFT);
  114. }
  115. iowrite8(b, dp->dma_mode);
  116. }
  117. static int atp867x_get_active_clocks_shifted(struct ata_port *ap,
  118. unsigned int clk)
  119. {
  120. struct atp867x_priv *dp = ap->private_data;
  121. unsigned char clocks = clk;
  122. /*
  123. * Doc 6.6.9: increase the clock value by 1 for safer PIO speed
  124. * on 66MHz bus
  125. */
  126. if (dp->pci66mhz)
  127. clocks++;
  128. switch (clocks) {
  129. case 0:
  130. clocks = 1;
  131. break;
  132. case 1 ... 6:
  133. break;
  134. default:
  135. printk(KERN_WARNING "ATP867X: active %dclk is invalid. "
  136. "Using 12clk.\n", clk);
  137. fallthrough;
  138. case 9 ... 12:
  139. clocks = 7; /* 12 clk */
  140. break;
  141. case 7:
  142. case 8: /* default 8 clk */
  143. clocks = 0;
  144. goto active_clock_shift_done;
  145. }
  146. active_clock_shift_done:
  147. return clocks << ATP867X_IO_PIOSPD_ACTIVE_SHIFT;
  148. }
  149. static int atp867x_get_recover_clocks_shifted(unsigned int clk)
  150. {
  151. unsigned char clocks = clk;
  152. switch (clocks) {
  153. case 0:
  154. clocks = 1;
  155. break;
  156. case 1 ... 11:
  157. break;
  158. case 13:
  159. case 14:
  160. --clocks; /* by the spec */
  161. break;
  162. case 15:
  163. break;
  164. default:
  165. printk(KERN_WARNING "ATP867X: recover %dclk is invalid. "
  166. "Using default 12clk.\n", clk);
  167. fallthrough;
  168. case 12: /* default 12 clk */
  169. clocks = 0;
  170. break;
  171. }
  172. return clocks << ATP867X_IO_PIOSPD_RECOVER_SHIFT;
  173. }
  174. static void atp867x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  175. {
  176. struct ata_device *peer = ata_dev_pair(adev);
  177. struct atp867x_priv *dp = ap->private_data;
  178. u8 speed = adev->pio_mode;
  179. struct ata_timing t, p;
  180. int T, UT;
  181. u8 b;
  182. T = 1000000000 / 33333;
  183. UT = T / 4;
  184. ata_timing_compute(adev, speed, &t, T, UT);
  185. if (peer && peer->pio_mode) {
  186. ata_timing_compute(peer, peer->pio_mode, &p, T, UT);
  187. ata_timing_merge(&p, &t, &t, ATA_TIMING_8BIT);
  188. }
  189. b = ioread8(dp->dma_mode);
  190. if (adev->devno & 1)
  191. b = (b & ~ATP867X_IO_DMAMODE_SLAVE_MASK);
  192. else
  193. b = (b & ~ATP867X_IO_DMAMODE_MSTR_MASK);
  194. iowrite8(b, dp->dma_mode);
  195. b = atp867x_get_active_clocks_shifted(ap, t.active) |
  196. atp867x_get_recover_clocks_shifted(t.recover);
  197. if (adev->devno & 1)
  198. iowrite8(b, dp->slave_piospd);
  199. else
  200. iowrite8(b, dp->mstr_piospd);
  201. b = atp867x_get_active_clocks_shifted(ap, t.act8b) |
  202. atp867x_get_recover_clocks_shifted(t.rec8b);
  203. iowrite8(b, dp->eightb_piospd);
  204. }
  205. static int atp867x_cable_override(struct pci_dev *pdev)
  206. {
  207. if (pdev->subsystem_vendor == PCI_VENDOR_ID_ARTOP &&
  208. (pdev->subsystem_device == PCI_DEVICE_ID_ARTOP_ATP867A ||
  209. pdev->subsystem_device == PCI_DEVICE_ID_ARTOP_ATP867B)) {
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. static int atp867x_cable_detect(struct ata_port *ap)
  215. {
  216. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  217. if (atp867x_cable_override(pdev))
  218. return ATA_CBL_PATA40_SHORT;
  219. return ATA_CBL_PATA_UNK;
  220. }
  221. static struct scsi_host_template atp867x_sht = {
  222. ATA_BMDMA_SHT(DRV_NAME),
  223. };
  224. static struct ata_port_operations atp867x_ops = {
  225. .inherits = &ata_bmdma_port_ops,
  226. .cable_detect = atp867x_cable_detect,
  227. .set_piomode = atp867x_set_piomode,
  228. .set_dmamode = atp867x_set_dmamode,
  229. };
  230. #ifdef ATP867X_DEBUG
  231. static void atp867x_check_res(struct pci_dev *pdev)
  232. {
  233. int i;
  234. unsigned long start, len;
  235. /* Check the PCI resources for this channel are enabled */
  236. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  237. start = pci_resource_start(pdev, i);
  238. len = pci_resource_len(pdev, i);
  239. printk(KERN_DEBUG "ATP867X: resource start:len=%lx:%lx\n",
  240. start, len);
  241. }
  242. }
  243. static void atp867x_check_ports(struct ata_port *ap, int port)
  244. {
  245. struct ata_ioports *ioaddr = &ap->ioaddr;
  246. struct atp867x_priv *dp = ap->private_data;
  247. printk(KERN_DEBUG "ATP867X: port[%d] addresses\n"
  248. " cmd_addr =0x%llx, 0x%llx\n"
  249. " ctl_addr =0x%llx, 0x%llx\n"
  250. " bmdma_addr =0x%llx, 0x%llx\n"
  251. " data_addr =0x%llx\n"
  252. " error_addr =0x%llx\n"
  253. " feature_addr =0x%llx\n"
  254. " nsect_addr =0x%llx\n"
  255. " lbal_addr =0x%llx\n"
  256. " lbam_addr =0x%llx\n"
  257. " lbah_addr =0x%llx\n"
  258. " device_addr =0x%llx\n"
  259. " status_addr =0x%llx\n"
  260. " command_addr =0x%llx\n"
  261. " dp->dma_mode =0x%llx\n"
  262. " dp->mstr_piospd =0x%llx\n"
  263. " dp->slave_piospd =0x%llx\n"
  264. " dp->eightb_piospd =0x%llx\n"
  265. " dp->pci66mhz =0x%lx\n",
  266. port,
  267. (unsigned long long)ioaddr->cmd_addr,
  268. (unsigned long long)ATP867X_IO_PORTBASE(ap, port),
  269. (unsigned long long)ioaddr->ctl_addr,
  270. (unsigned long long)ATP867X_IO_ALTSTATUS(ap, port),
  271. (unsigned long long)ioaddr->bmdma_addr,
  272. (unsigned long long)ATP867X_IO_DMABASE(ap, port),
  273. (unsigned long long)ioaddr->data_addr,
  274. (unsigned long long)ioaddr->error_addr,
  275. (unsigned long long)ioaddr->feature_addr,
  276. (unsigned long long)ioaddr->nsect_addr,
  277. (unsigned long long)ioaddr->lbal_addr,
  278. (unsigned long long)ioaddr->lbam_addr,
  279. (unsigned long long)ioaddr->lbah_addr,
  280. (unsigned long long)ioaddr->device_addr,
  281. (unsigned long long)ioaddr->status_addr,
  282. (unsigned long long)ioaddr->command_addr,
  283. (unsigned long long)dp->dma_mode,
  284. (unsigned long long)dp->mstr_piospd,
  285. (unsigned long long)dp->slave_piospd,
  286. (unsigned long long)dp->eightb_piospd,
  287. (unsigned long)dp->pci66mhz);
  288. }
  289. #endif
  290. static int atp867x_set_priv(struct ata_port *ap)
  291. {
  292. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  293. struct atp867x_priv *dp;
  294. int port = ap->port_no;
  295. dp = ap->private_data =
  296. devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
  297. if (dp == NULL)
  298. return -ENOMEM;
  299. dp->dma_mode = ATP867X_IO_DMAMODE(ap, port);
  300. dp->mstr_piospd = ATP867X_IO_MSTRPIOSPD(ap, port);
  301. dp->slave_piospd = ATP867X_IO_SLAVPIOSPD(ap, port);
  302. dp->eightb_piospd = ATP867X_IO_8BPIOSPD(ap, port);
  303. dp->pci66mhz =
  304. ioread8(ATP867X_SYS_INFO(ap)) & ATP867X_IO_SYS_INFO_66MHZ;
  305. return 0;
  306. }
  307. static void atp867x_fixup(struct ata_host *host)
  308. {
  309. struct pci_dev *pdev = to_pci_dev(host->dev);
  310. struct ata_port *ap = host->ports[0];
  311. int i;
  312. u8 v;
  313. /*
  314. * Broken BIOS might not set latency high enough
  315. */
  316. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &v);
  317. if (v < 0x80) {
  318. v = 0x80;
  319. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, v);
  320. printk(KERN_DEBUG "ATP867X: set latency timer of device %s"
  321. " to %d\n", pci_name(pdev), v);
  322. }
  323. /*
  324. * init 8bit io ports speed(0aaarrrr) to 43h and
  325. * init udma modes of master/slave to 0/0(11h)
  326. */
  327. for (i = 0; i < ATP867X_NUM_PORTS; i++)
  328. iowrite16(ATP867X_IO_PORTSPD_VAL, ATP867X_IO_PORTSPD(ap, i));
  329. /*
  330. * init PreREAD counts
  331. */
  332. for (i = 0; i < ATP867X_NUM_PORTS; i++)
  333. iowrite16(ATP867X_PREREAD_VAL, ATP867X_IO_PREREAD(ap, i));
  334. v = ioread8(ATP867X_IOBASE(ap) + 0x28);
  335. v &= 0xcf; /* Enable INTA#: bit4=0 means enable */
  336. v |= 0xc0; /* Enable PCI burst, MRM & not immediate interrupts */
  337. iowrite8(v, ATP867X_IOBASE(ap) + 0x28);
  338. /*
  339. * Turn off the over clocked udma5 mode, only for Rev-B
  340. */
  341. v = ioread8(ATP867X_SYS_INFO(ap));
  342. v &= ATP867X_IO_SYS_MASK_RESERVED;
  343. if (pdev->device == PCI_DEVICE_ID_ARTOP_ATP867B)
  344. v |= ATP867X_IO_SYS_INFO_SLOW_UDMA5;
  345. iowrite8(v, ATP867X_SYS_INFO(ap));
  346. }
  347. static int atp867x_ata_pci_sff_init_host(struct ata_host *host)
  348. {
  349. struct device *gdev = host->dev;
  350. struct pci_dev *pdev = to_pci_dev(gdev);
  351. unsigned int mask = 0;
  352. int i, rc;
  353. /*
  354. * do not map rombase
  355. */
  356. rc = pcim_iomap_regions(pdev, 1 << ATP867X_BAR_IOBASE, DRV_NAME);
  357. if (rc == -EBUSY)
  358. pcim_pin_device(pdev);
  359. if (rc)
  360. return rc;
  361. host->iomap = pcim_iomap_table(pdev);
  362. #ifdef ATP867X_DEBUG
  363. atp867x_check_res(pdev);
  364. for (i = 0; i < PCI_STD_NUM_BARS; i++)
  365. printk(KERN_DEBUG "ATP867X: iomap[%d]=0x%llx\n", i,
  366. (unsigned long long)(host->iomap[i]));
  367. #endif
  368. /*
  369. * request, iomap BARs and init port addresses accordingly
  370. */
  371. for (i = 0; i < host->n_ports; i++) {
  372. struct ata_port *ap = host->ports[i];
  373. struct ata_ioports *ioaddr = &ap->ioaddr;
  374. ioaddr->cmd_addr = ATP867X_IO_PORTBASE(ap, i);
  375. ioaddr->ctl_addr = ioaddr->altstatus_addr
  376. = ATP867X_IO_ALTSTATUS(ap, i);
  377. ioaddr->bmdma_addr = ATP867X_IO_DMABASE(ap, i);
  378. ata_sff_std_ports(ioaddr);
  379. rc = atp867x_set_priv(ap);
  380. if (rc)
  381. return rc;
  382. #ifdef ATP867X_DEBUG
  383. atp867x_check_ports(ap, i);
  384. #endif
  385. ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx",
  386. (unsigned long)ioaddr->cmd_addr,
  387. (unsigned long)ioaddr->ctl_addr);
  388. ata_port_desc(ap, "bmdma 0x%lx",
  389. (unsigned long)ioaddr->bmdma_addr);
  390. mask |= 1 << i;
  391. }
  392. if (!mask) {
  393. dev_err(gdev, "no available native port\n");
  394. return -ENODEV;
  395. }
  396. atp867x_fixup(host);
  397. return dma_set_mask_and_coherent(&pdev->dev, ATA_DMA_MASK);
  398. }
  399. static int atp867x_init_one(struct pci_dev *pdev,
  400. const struct pci_device_id *id)
  401. {
  402. static const struct ata_port_info info_867x = {
  403. .flags = ATA_FLAG_SLAVE_POSS,
  404. .pio_mask = ATA_PIO4,
  405. .udma_mask = ATA_UDMA6,
  406. .port_ops = &atp867x_ops,
  407. };
  408. struct ata_host *host;
  409. const struct ata_port_info *ppi[] = { &info_867x, NULL };
  410. int rc;
  411. ata_print_version_once(&pdev->dev, DRV_VERSION);
  412. rc = pcim_enable_device(pdev);
  413. if (rc)
  414. return rc;
  415. printk(KERN_INFO "ATP867X: ATP867 ATA UDMA133 controller (rev %02X)",
  416. pdev->device);
  417. host = ata_host_alloc_pinfo(&pdev->dev, ppi, ATP867X_NUM_PORTS);
  418. if (!host) {
  419. dev_err(&pdev->dev, "failed to allocate ATA host\n");
  420. rc = -ENOMEM;
  421. goto err_out;
  422. }
  423. rc = atp867x_ata_pci_sff_init_host(host);
  424. if (rc) {
  425. dev_err(&pdev->dev, "failed to init host\n");
  426. goto err_out;
  427. }
  428. pci_set_master(pdev);
  429. rc = ata_host_activate(host, pdev->irq, ata_bmdma_interrupt,
  430. IRQF_SHARED, &atp867x_sht);
  431. if (rc)
  432. dev_err(&pdev->dev, "failed to activate host\n");
  433. err_out:
  434. return rc;
  435. }
  436. #ifdef CONFIG_PM_SLEEP
  437. static int atp867x_reinit_one(struct pci_dev *pdev)
  438. {
  439. struct ata_host *host = pci_get_drvdata(pdev);
  440. int rc;
  441. rc = ata_pci_device_do_resume(pdev);
  442. if (rc)
  443. return rc;
  444. atp867x_fixup(host);
  445. ata_host_resume(host);
  446. return 0;
  447. }
  448. #endif
  449. static struct pci_device_id atp867x_pci_tbl[] = {
  450. { PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867A), 0 },
  451. { PCI_VDEVICE(ARTOP, PCI_DEVICE_ID_ARTOP_ATP867B), 0 },
  452. { },
  453. };
  454. static struct pci_driver atp867x_driver = {
  455. .name = DRV_NAME,
  456. .id_table = atp867x_pci_tbl,
  457. .probe = atp867x_init_one,
  458. .remove = ata_pci_remove_one,
  459. #ifdef CONFIG_PM_SLEEP
  460. .suspend = ata_pci_device_suspend,
  461. .resume = atp867x_reinit_one,
  462. #endif
  463. };
  464. module_pci_driver(atp867x_driver);
  465. MODULE_AUTHOR("John(Jung-Ik) Lee, Google Inc.");
  466. MODULE_DESCRIPTION("low level driver for Artop/Acard 867x ATA controller");
  467. MODULE_LICENSE("GPL");
  468. MODULE_DEVICE_TABLE(pci, atp867x_pci_tbl);
  469. MODULE_VERSION(DRV_VERSION);