sun_esp.c 14 KB

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