sun_esp.c 14 KB

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