sun_esp.c 14 KB

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