sun_esp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* sun_esp.c: ESP front-end for Sparc SBUS systems.
  3. *
  4. * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net)
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/types.h>
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/init.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/gfp.h>
  17. #include <asm/irq.h>
  18. #include <asm/io.h>
  19. #include <asm/dma.h>
  20. #include <scsi/scsi_host.h>
  21. #include "esp_scsi.h"
  22. #define DRV_MODULE_NAME "sun_esp"
  23. #define PFX DRV_MODULE_NAME ": "
  24. #define DRV_VERSION "1.100"
  25. #define DRV_MODULE_RELDATE "August 27, 2008"
  26. #define dma_read32(REG) \
  27. sbus_readl(esp->dma_regs + (REG))
  28. #define dma_write32(VAL, REG) \
  29. sbus_writel((VAL), esp->dma_regs + (REG))
  30. /* DVMA chip revisions */
  31. enum dvma_rev {
  32. dvmarev0,
  33. dvmaesc1,
  34. dvmarev1,
  35. dvmarev2,
  36. dvmarev3,
  37. dvmarevplus,
  38. dvmahme
  39. };
  40. static int esp_sbus_setup_dma(struct esp *esp, struct platform_device *dma_of)
  41. {
  42. esp->dma = dma_of;
  43. esp->dma_regs = of_ioremap(&dma_of->resource[0], 0,
  44. resource_size(&dma_of->resource[0]),
  45. "espdma");
  46. if (!esp->dma_regs)
  47. return -ENOMEM;
  48. switch (dma_read32(DMA_CSR) & DMA_DEVICE_ID) {
  49. case DMA_VERS0:
  50. esp->dmarev = dvmarev0;
  51. break;
  52. case DMA_ESCV1:
  53. esp->dmarev = dvmaesc1;
  54. break;
  55. case DMA_VERS1:
  56. esp->dmarev = dvmarev1;
  57. break;
  58. case DMA_VERS2:
  59. esp->dmarev = dvmarev2;
  60. break;
  61. case DMA_VERHME:
  62. esp->dmarev = dvmahme;
  63. break;
  64. case DMA_VERSPLUS:
  65. esp->dmarev = dvmarevplus;
  66. break;
  67. }
  68. return 0;
  69. }
  70. static int esp_sbus_map_regs(struct esp *esp, int hme)
  71. {
  72. struct platform_device *op = to_platform_device(esp->dev);
  73. struct resource *res;
  74. /* On HME, two reg sets exist, first is DVMA,
  75. * second is ESP registers.
  76. */
  77. if (hme)
  78. res = &op->resource[1];
  79. else
  80. res = &op->resource[0];
  81. esp->regs = of_ioremap(res, 0, SBUS_ESP_REG_SIZE, "ESP");
  82. if (!esp->regs)
  83. return -ENOMEM;
  84. return 0;
  85. }
  86. static int esp_sbus_map_command_block(struct esp *esp)
  87. {
  88. esp->command_block = dma_alloc_coherent(esp->dev, 16,
  89. &esp->command_block_dma,
  90. GFP_KERNEL);
  91. if (!esp->command_block)
  92. return -ENOMEM;
  93. return 0;
  94. }
  95. static int esp_sbus_register_irq(struct esp *esp)
  96. {
  97. struct Scsi_Host *host = esp->host;
  98. struct platform_device *op = to_platform_device(esp->dev);
  99. host->irq = op->archdata.irqs[0];
  100. return request_irq(host->irq, scsi_esp_intr, IRQF_SHARED, "ESP", esp);
  101. }
  102. static void esp_get_scsi_id(struct esp *esp, struct platform_device *espdma)
  103. {
  104. struct platform_device *op = to_platform_device(esp->dev);
  105. struct device_node *dp;
  106. dp = op->dev.of_node;
  107. esp->scsi_id = of_getintprop_default(dp, "initiator-id", 0xff);
  108. if (esp->scsi_id != 0xff)
  109. goto done;
  110. esp->scsi_id = of_getintprop_default(dp, "scsi-initiator-id", 0xff);
  111. if (esp->scsi_id != 0xff)
  112. goto done;
  113. esp->scsi_id = of_getintprop_default(espdma->dev.of_node,
  114. "scsi-initiator-id", 7);
  115. done:
  116. esp->host->this_id = esp->scsi_id;
  117. esp->scsi_id_mask = (1 << esp->scsi_id);
  118. }
  119. static void esp_get_differential(struct esp *esp)
  120. {
  121. struct platform_device *op = to_platform_device(esp->dev);
  122. struct device_node *dp;
  123. dp = op->dev.of_node;
  124. if (of_property_read_bool(dp, "differential"))
  125. esp->flags |= ESP_FLAG_DIFFERENTIAL;
  126. else
  127. esp->flags &= ~ESP_FLAG_DIFFERENTIAL;
  128. }
  129. static void esp_get_clock_params(struct esp *esp)
  130. {
  131. struct platform_device *op = to_platform_device(esp->dev);
  132. struct device_node *bus_dp, *dp;
  133. int fmhz;
  134. dp = op->dev.of_node;
  135. bus_dp = dp->parent;
  136. fmhz = of_getintprop_default(dp, "clock-frequency", 0);
  137. if (fmhz == 0)
  138. fmhz = of_getintprop_default(bus_dp, "clock-frequency", 0);
  139. esp->cfreq = fmhz;
  140. }
  141. static void esp_get_bursts(struct esp *esp, struct platform_device *dma_of)
  142. {
  143. struct device_node *dma_dp = dma_of->dev.of_node;
  144. struct platform_device *op = to_platform_device(esp->dev);
  145. struct device_node *dp;
  146. u8 bursts, val;
  147. dp = op->dev.of_node;
  148. bursts = of_getintprop_default(dp, "burst-sizes", 0xff);
  149. val = of_getintprop_default(dma_dp, "burst-sizes", 0xff);
  150. if (val != 0xff)
  151. bursts &= val;
  152. val = of_getintprop_default(dma_dp->parent, "burst-sizes", 0xff);
  153. if (val != 0xff)
  154. bursts &= val;
  155. if (bursts == 0xff ||
  156. (bursts & DMA_BURST16) == 0 ||
  157. (bursts & DMA_BURST32) == 0)
  158. bursts = (DMA_BURST32 - 1);
  159. esp->bursts = bursts;
  160. }
  161. static void esp_sbus_get_props(struct esp *esp, struct platform_device *espdma)
  162. {
  163. esp_get_scsi_id(esp, espdma);
  164. esp_get_differential(esp);
  165. esp_get_clock_params(esp);
  166. esp_get_bursts(esp, espdma);
  167. }
  168. static void sbus_esp_write8(struct esp *esp, u8 val, unsigned long reg)
  169. {
  170. sbus_writeb(val, esp->regs + (reg * 4UL));
  171. }
  172. static u8 sbus_esp_read8(struct esp *esp, unsigned long reg)
  173. {
  174. return sbus_readb(esp->regs + (reg * 4UL));
  175. }
  176. static int sbus_esp_irq_pending(struct esp *esp)
  177. {
  178. if (dma_read32(DMA_CSR) & (DMA_HNDL_INTR | DMA_HNDL_ERROR))
  179. return 1;
  180. return 0;
  181. }
  182. static void sbus_esp_reset_dma(struct esp *esp)
  183. {
  184. int can_do_burst16, can_do_burst32, can_do_burst64;
  185. int can_do_sbus64, lim;
  186. struct platform_device *op = to_platform_device(esp->dev);
  187. u32 val;
  188. can_do_burst16 = (esp->bursts & DMA_BURST16) != 0;
  189. can_do_burst32 = (esp->bursts & DMA_BURST32) != 0;
  190. can_do_burst64 = 0;
  191. can_do_sbus64 = 0;
  192. if (sbus_can_dma_64bit())
  193. can_do_sbus64 = 1;
  194. if (sbus_can_burst64())
  195. can_do_burst64 = (esp->bursts & DMA_BURST64) != 0;
  196. /* Put the DVMA into a known state. */
  197. if (esp->dmarev != dvmahme) {
  198. val = dma_read32(DMA_CSR);
  199. dma_write32(val | DMA_RST_SCSI, DMA_CSR);
  200. dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
  201. }
  202. switch (esp->dmarev) {
  203. case dvmahme:
  204. dma_write32(DMA_RESET_FAS366, DMA_CSR);
  205. dma_write32(DMA_RST_SCSI, DMA_CSR);
  206. esp->prev_hme_dmacsr = (DMA_PARITY_OFF | DMA_2CLKS |
  207. DMA_SCSI_DISAB | DMA_INT_ENAB);
  208. esp->prev_hme_dmacsr &= ~(DMA_ENABLE | DMA_ST_WRITE |
  209. DMA_BRST_SZ);
  210. if (can_do_burst64)
  211. esp->prev_hme_dmacsr |= DMA_BRST64;
  212. else if (can_do_burst32)
  213. esp->prev_hme_dmacsr |= DMA_BRST32;
  214. if (can_do_sbus64) {
  215. esp->prev_hme_dmacsr |= DMA_SCSI_SBUS64;
  216. sbus_set_sbus64(&op->dev, esp->bursts);
  217. }
  218. lim = 1000;
  219. while (dma_read32(DMA_CSR) & DMA_PEND_READ) {
  220. if (--lim == 0) {
  221. printk(KERN_ALERT PFX "esp%d: DMA_PEND_READ "
  222. "will not clear!\n",
  223. esp->host->unique_id);
  224. break;
  225. }
  226. udelay(1);
  227. }
  228. dma_write32(0, DMA_CSR);
  229. dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
  230. dma_write32(0, DMA_ADDR);
  231. break;
  232. case dvmarev2:
  233. if (esp->rev != ESP100) {
  234. val = dma_read32(DMA_CSR);
  235. dma_write32(val | DMA_3CLKS, DMA_CSR);
  236. }
  237. break;
  238. case dvmarev3:
  239. val = dma_read32(DMA_CSR);
  240. val &= ~DMA_3CLKS;
  241. val |= DMA_2CLKS;
  242. if (can_do_burst32) {
  243. val &= ~DMA_BRST_SZ;
  244. val |= DMA_BRST32;
  245. }
  246. dma_write32(val, DMA_CSR);
  247. break;
  248. case dvmaesc1:
  249. val = dma_read32(DMA_CSR);
  250. val |= DMA_ADD_ENABLE;
  251. val &= ~DMA_BCNT_ENAB;
  252. if (!can_do_burst32 && can_do_burst16) {
  253. val |= DMA_ESC_BURST;
  254. } else {
  255. val &= ~(DMA_ESC_BURST);
  256. }
  257. dma_write32(val, DMA_CSR);
  258. break;
  259. default:
  260. break;
  261. }
  262. /* Enable interrupts. */
  263. val = dma_read32(DMA_CSR);
  264. dma_write32(val | DMA_INT_ENAB, DMA_CSR);
  265. }
  266. static void sbus_esp_dma_drain(struct esp *esp)
  267. {
  268. u32 csr;
  269. int lim;
  270. if (esp->dmarev == dvmahme)
  271. return;
  272. csr = dma_read32(DMA_CSR);
  273. if (!(csr & DMA_FIFO_ISDRAIN))
  274. return;
  275. if (esp->dmarev != dvmarev3 && esp->dmarev != dvmaesc1)
  276. dma_write32(csr | DMA_FIFO_STDRAIN, DMA_CSR);
  277. lim = 1000;
  278. while (dma_read32(DMA_CSR) & DMA_FIFO_ISDRAIN) {
  279. if (--lim == 0) {
  280. printk(KERN_ALERT PFX "esp%d: DMA will not drain!\n",
  281. esp->host->unique_id);
  282. break;
  283. }
  284. udelay(1);
  285. }
  286. }
  287. static void sbus_esp_dma_invalidate(struct esp *esp)
  288. {
  289. if (esp->dmarev == dvmahme) {
  290. dma_write32(DMA_RST_SCSI, DMA_CSR);
  291. esp->prev_hme_dmacsr = ((esp->prev_hme_dmacsr |
  292. (DMA_PARITY_OFF | DMA_2CLKS |
  293. DMA_SCSI_DISAB | DMA_INT_ENAB)) &
  294. ~(DMA_ST_WRITE | DMA_ENABLE));
  295. dma_write32(0, DMA_CSR);
  296. dma_write32(esp->prev_hme_dmacsr, DMA_CSR);
  297. /* This is necessary to avoid having the SCSI channel
  298. * engine lock up on us.
  299. */
  300. dma_write32(0, DMA_ADDR);
  301. } else {
  302. u32 val;
  303. int lim;
  304. lim = 1000;
  305. while ((val = dma_read32(DMA_CSR)) & DMA_PEND_READ) {
  306. if (--lim == 0) {
  307. printk(KERN_ALERT PFX "esp%d: DMA will not "
  308. "invalidate!\n", esp->host->unique_id);
  309. break;
  310. }
  311. udelay(1);
  312. }
  313. val &= ~(DMA_ENABLE | DMA_ST_WRITE | DMA_BCNT_ENAB);
  314. val |= DMA_FIFO_INV;
  315. dma_write32(val, DMA_CSR);
  316. val &= ~DMA_FIFO_INV;
  317. dma_write32(val, DMA_CSR);
  318. }
  319. }
  320. static void sbus_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
  321. u32 dma_count, int write, u8 cmd)
  322. {
  323. u32 csr;
  324. BUG_ON(!(cmd & ESP_CMD_DMA));
  325. sbus_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  326. sbus_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  327. if (esp->rev == FASHME) {
  328. sbus_esp_write8(esp, (esp_count >> 16) & 0xff, FAS_RLO);
  329. sbus_esp_write8(esp, 0, FAS_RHI);
  330. scsi_esp_cmd(esp, cmd);
  331. csr = esp->prev_hme_dmacsr;
  332. csr |= DMA_SCSI_DISAB | DMA_ENABLE;
  333. if (write)
  334. csr |= DMA_ST_WRITE;
  335. else
  336. csr &= ~DMA_ST_WRITE;
  337. esp->prev_hme_dmacsr = csr;
  338. dma_write32(dma_count, DMA_COUNT);
  339. dma_write32(addr, DMA_ADDR);
  340. dma_write32(csr, DMA_CSR);
  341. } else {
  342. csr = dma_read32(DMA_CSR);
  343. csr |= DMA_ENABLE;
  344. if (write)
  345. csr |= DMA_ST_WRITE;
  346. else
  347. csr &= ~DMA_ST_WRITE;
  348. dma_write32(csr, DMA_CSR);
  349. if (esp->dmarev == dvmaesc1) {
  350. u32 end = PAGE_ALIGN(addr + dma_count + 16U);
  351. dma_write32(end - addr, DMA_COUNT);
  352. }
  353. dma_write32(addr, DMA_ADDR);
  354. scsi_esp_cmd(esp, cmd);
  355. }
  356. }
  357. static int sbus_esp_dma_error(struct esp *esp)
  358. {
  359. u32 csr = dma_read32(DMA_CSR);
  360. if (csr & DMA_HNDL_ERROR)
  361. return 1;
  362. return 0;
  363. }
  364. static const struct esp_driver_ops sbus_esp_ops = {
  365. .esp_write8 = sbus_esp_write8,
  366. .esp_read8 = sbus_esp_read8,
  367. .irq_pending = sbus_esp_irq_pending,
  368. .reset_dma = sbus_esp_reset_dma,
  369. .dma_drain = sbus_esp_dma_drain,
  370. .dma_invalidate = sbus_esp_dma_invalidate,
  371. .send_dma_cmd = sbus_esp_send_dma_cmd,
  372. .dma_error = sbus_esp_dma_error,
  373. };
  374. static int esp_sbus_probe_one(struct platform_device *op,
  375. struct platform_device *espdma, int hme)
  376. {
  377. const struct scsi_host_template *tpnt = &scsi_esp_template;
  378. struct Scsi_Host *host;
  379. struct esp *esp;
  380. int err;
  381. host = scsi_host_alloc(tpnt, sizeof(struct esp));
  382. err = -ENOMEM;
  383. if (!host)
  384. goto fail;
  385. host->max_id = (hme ? 16 : 8);
  386. esp = shost_priv(host);
  387. esp->host = host;
  388. esp->dev = &op->dev;
  389. esp->ops = &sbus_esp_ops;
  390. if (hme)
  391. esp->flags |= ESP_FLAG_WIDE_CAPABLE;
  392. err = esp_sbus_setup_dma(esp, espdma);
  393. if (err < 0)
  394. goto fail_unlink;
  395. err = esp_sbus_map_regs(esp, hme);
  396. if (err < 0)
  397. goto fail_unlink;
  398. err = esp_sbus_map_command_block(esp);
  399. if (err < 0)
  400. goto fail_unmap_regs;
  401. err = esp_sbus_register_irq(esp);
  402. if (err < 0)
  403. goto fail_unmap_command_block;
  404. esp_sbus_get_props(esp, espdma);
  405. /* Before we try to touch the ESP chip, ESC1 dma can
  406. * come up with the reset bit set, so make sure that
  407. * is clear first.
  408. */
  409. if (esp->dmarev == dvmaesc1) {
  410. u32 val = dma_read32(DMA_CSR);
  411. dma_write32(val & ~DMA_RST_SCSI, DMA_CSR);
  412. }
  413. dev_set_drvdata(&op->dev, esp);
  414. err = scsi_esp_register(esp);
  415. if (err)
  416. goto fail_free_irq;
  417. return 0;
  418. fail_free_irq:
  419. free_irq(host->irq, esp);
  420. fail_unmap_command_block:
  421. dma_free_coherent(&op->dev, 16,
  422. esp->command_block,
  423. esp->command_block_dma);
  424. fail_unmap_regs:
  425. of_iounmap(&op->resource[(hme ? 1 : 0)], esp->regs, SBUS_ESP_REG_SIZE);
  426. fail_unlink:
  427. scsi_host_put(host);
  428. fail:
  429. return err;
  430. }
  431. static int esp_sbus_probe(struct platform_device *op)
  432. {
  433. struct device_node *dma_node = NULL;
  434. struct device_node *dp = op->dev.of_node;
  435. struct platform_device *dma_of = NULL;
  436. int hme = 0;
  437. int ret;
  438. if (of_node_name_eq(dp->parent, "espdma") ||
  439. of_node_name_eq(dp->parent, "dma"))
  440. dma_node = dp->parent;
  441. else if (of_node_name_eq(dp, "SUNW,fas")) {
  442. dma_node = op->dev.of_node;
  443. hme = 1;
  444. }
  445. if (dma_node)
  446. dma_of = of_find_device_by_node(dma_node);
  447. if (!dma_of)
  448. return -ENODEV;
  449. ret = esp_sbus_probe_one(op, dma_of, hme);
  450. if (ret)
  451. put_device(&dma_of->dev);
  452. return ret;
  453. }
  454. static void esp_sbus_remove(struct platform_device *op)
  455. {
  456. struct esp *esp = dev_get_drvdata(&op->dev);
  457. struct platform_device *dma_of = esp->dma;
  458. unsigned int irq = esp->host->irq;
  459. bool is_hme;
  460. u32 val;
  461. scsi_esp_unregister(esp);
  462. /* Disable interrupts. */
  463. val = dma_read32(DMA_CSR);
  464. dma_write32(val & ~DMA_INT_ENAB, DMA_CSR);
  465. free_irq(irq, esp);
  466. is_hme = (esp->dmarev == dvmahme);
  467. dma_free_coherent(&op->dev, 16,
  468. esp->command_block,
  469. esp->command_block_dma);
  470. of_iounmap(&op->resource[(is_hme ? 1 : 0)], esp->regs,
  471. SBUS_ESP_REG_SIZE);
  472. of_iounmap(&dma_of->resource[0], esp->dma_regs,
  473. resource_size(&dma_of->resource[0]));
  474. scsi_host_put(esp->host);
  475. dev_set_drvdata(&op->dev, NULL);
  476. put_device(&dma_of->dev);
  477. }
  478. static const struct of_device_id esp_match[] = {
  479. {
  480. .name = "SUNW,esp",
  481. },
  482. {
  483. .name = "SUNW,fas",
  484. },
  485. {
  486. .name = "esp",
  487. },
  488. {},
  489. };
  490. MODULE_DEVICE_TABLE(of, esp_match);
  491. static struct platform_driver esp_sbus_driver = {
  492. .driver = {
  493. .name = "esp",
  494. .of_match_table = esp_match,
  495. },
  496. .probe = esp_sbus_probe,
  497. .remove = esp_sbus_remove,
  498. };
  499. module_platform_driver(esp_sbus_driver);
  500. MODULE_DESCRIPTION("Sun ESP SCSI driver");
  501. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  502. MODULE_LICENSE("GPL");
  503. MODULE_VERSION(DRV_VERSION);