PageRenderTime 73ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/ieee1394/ohci1394.c

https://bitbucket.org/evzijst/gittest
C | 3705 lines | 2512 code | 685 blank | 508 comment | 415 complexity | 1b44604c45ed287f0ac5dea7f1e3021c MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * ohci1394.c - driver for OHCI 1394 boards
  3. * Copyright (C)1999,2000 Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>
  4. * Gord Peters <GordPeters@smarttech.com>
  5. * 2001 Ben Collins <bcollins@debian.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. /*
  22. * Things known to be working:
  23. * . Async Request Transmit
  24. * . Async Response Receive
  25. * . Async Request Receive
  26. * . Async Response Transmit
  27. * . Iso Receive
  28. * . DMA mmap for iso receive
  29. * . Config ROM generation
  30. *
  31. * Things implemented, but still in test phase:
  32. * . Iso Transmit
  33. * . Async Stream Packets Transmit (Receive done via Iso interface)
  34. *
  35. * Things not implemented:
  36. * . DMA error recovery
  37. *
  38. * Known bugs:
  39. * . devctl BUS_RESET arg confusion (reset type or root holdoff?)
  40. * added LONG_RESET_ROOT and SHORT_RESET_ROOT for root holdoff --kk
  41. */
  42. /*
  43. * Acknowledgments:
  44. *
  45. * Adam J Richter <adam@yggdrasil.com>
  46. * . Use of pci_class to find device
  47. *
  48. * Emilie Chung <emilie.chung@axis.com>
  49. * . Tip on Async Request Filter
  50. *
  51. * Pascal Drolet <pascal.drolet@informission.ca>
  52. * . Various tips for optimization and functionnalities
  53. *
  54. * Robert Ficklin <rficklin@westengineering.com>
  55. * . Loop in irq_handler
  56. *
  57. * James Goodwin <jamesg@Filanet.com>
  58. * . Various tips on initialization, self-id reception, etc.
  59. *
  60. * Albrecht Dress <ad@mpifr-bonn.mpg.de>
  61. * . Apple PowerBook detection
  62. *
  63. * Daniel Kobras <daniel.kobras@student.uni-tuebingen.de>
  64. * . Reset the board properly before leaving + misc cleanups
  65. *
  66. * Leon van Stuivenberg <leonvs@iae.nl>
  67. * . Bug fixes
  68. *
  69. * Ben Collins <bcollins@debian.org>
  70. * . Working big-endian support
  71. * . Updated to 2.4.x module scheme (PCI aswell)
  72. * . Config ROM generation
  73. *
  74. * Manfred Weihs <weihs@ict.tuwien.ac.at>
  75. * . Reworked code for initiating bus resets
  76. * (long, short, with or without hold-off)
  77. *
  78. * Nandu Santhi <contactnandu@users.sourceforge.net>
  79. * . Added support for nVidia nForce2 onboard Firewire chipset
  80. *
  81. */
  82. #include <linux/config.h>
  83. #include <linux/kernel.h>
  84. #include <linux/list.h>
  85. #include <linux/slab.h>
  86. #include <linux/interrupt.h>
  87. #include <linux/wait.h>
  88. #include <linux/errno.h>
  89. #include <linux/module.h>
  90. #include <linux/moduleparam.h>
  91. #include <linux/pci.h>
  92. #include <linux/fs.h>
  93. #include <linux/poll.h>
  94. #include <asm/byteorder.h>
  95. #include <asm/atomic.h>
  96. #include <asm/uaccess.h>
  97. #include <linux/delay.h>
  98. #include <linux/spinlock.h>
  99. #include <asm/pgtable.h>
  100. #include <asm/page.h>
  101. #include <asm/irq.h>
  102. #include <linux/sched.h>
  103. #include <linux/types.h>
  104. #include <linux/vmalloc.h>
  105. #include <linux/init.h>
  106. #ifdef CONFIG_PPC_PMAC
  107. #include <asm/machdep.h>
  108. #include <asm/pmac_feature.h>
  109. #include <asm/prom.h>
  110. #include <asm/pci-bridge.h>
  111. #endif
  112. #include "csr1212.h"
  113. #include "ieee1394.h"
  114. #include "ieee1394_types.h"
  115. #include "hosts.h"
  116. #include "dma.h"
  117. #include "iso.h"
  118. #include "ieee1394_core.h"
  119. #include "highlevel.h"
  120. #include "ohci1394.h"
  121. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  122. #define OHCI1394_DEBUG
  123. #endif
  124. #ifdef DBGMSG
  125. #undef DBGMSG
  126. #endif
  127. #ifdef OHCI1394_DEBUG
  128. #define DBGMSG(fmt, args...) \
  129. printk(KERN_INFO "%s: fw-host%d: " fmt "\n" , OHCI1394_DRIVER_NAME, ohci->host->id , ## args)
  130. #else
  131. #define DBGMSG(fmt, args...)
  132. #endif
  133. #ifdef CONFIG_IEEE1394_OHCI_DMA_DEBUG
  134. #define OHCI_DMA_ALLOC(fmt, args...) \
  135. HPSB_ERR("%s(%s)alloc(%d): "fmt, OHCI1394_DRIVER_NAME, __FUNCTION__, \
  136. ++global_outstanding_dmas, ## args)
  137. #define OHCI_DMA_FREE(fmt, args...) \
  138. HPSB_ERR("%s(%s)free(%d): "fmt, OHCI1394_DRIVER_NAME, __FUNCTION__, \
  139. --global_outstanding_dmas, ## args)
  140. static int global_outstanding_dmas = 0;
  141. #else
  142. #define OHCI_DMA_ALLOC(fmt, args...)
  143. #define OHCI_DMA_FREE(fmt, args...)
  144. #endif
  145. /* print general (card independent) information */
  146. #define PRINT_G(level, fmt, args...) \
  147. printk(level "%s: " fmt "\n" , OHCI1394_DRIVER_NAME , ## args)
  148. /* print card specific information */
  149. #define PRINT(level, fmt, args...) \
  150. printk(level "%s: fw-host%d: " fmt "\n" , OHCI1394_DRIVER_NAME, ohci->host->id , ## args)
  151. static char version[] __devinitdata =
  152. "$Rev: 1250 $ Ben Collins <bcollins@debian.org>";
  153. /* Module Parameters */
  154. static int phys_dma = 1;
  155. module_param(phys_dma, int, 0644);
  156. MODULE_PARM_DESC(phys_dma, "Enable physical dma (default = 1).");
  157. static void dma_trm_tasklet(unsigned long data);
  158. static void dma_trm_reset(struct dma_trm_ctx *d);
  159. static int alloc_dma_rcv_ctx(struct ti_ohci *ohci, struct dma_rcv_ctx *d,
  160. enum context_type type, int ctx, int num_desc,
  161. int buf_size, int split_buf_size, int context_base);
  162. static void stop_dma_rcv_ctx(struct dma_rcv_ctx *d);
  163. static void free_dma_rcv_ctx(struct dma_rcv_ctx *d);
  164. static int alloc_dma_trm_ctx(struct ti_ohci *ohci, struct dma_trm_ctx *d,
  165. enum context_type type, int ctx, int num_desc,
  166. int context_base);
  167. static void ohci1394_pci_remove(struct pci_dev *pdev);
  168. #ifndef __LITTLE_ENDIAN
  169. static unsigned hdr_sizes[] =
  170. {
  171. 3, /* TCODE_WRITEQ */
  172. 4, /* TCODE_WRITEB */
  173. 3, /* TCODE_WRITE_RESPONSE */
  174. 0, /* ??? */
  175. 3, /* TCODE_READQ */
  176. 4, /* TCODE_READB */
  177. 3, /* TCODE_READQ_RESPONSE */
  178. 4, /* TCODE_READB_RESPONSE */
  179. 1, /* TCODE_CYCLE_START (???) */
  180. 4, /* TCODE_LOCK_REQUEST */
  181. 2, /* TCODE_ISO_DATA */
  182. 4, /* TCODE_LOCK_RESPONSE */
  183. };
  184. /* Swap headers */
  185. static inline void packet_swab(quadlet_t *data, int tcode)
  186. {
  187. size_t size = hdr_sizes[tcode];
  188. if (tcode > TCODE_LOCK_RESPONSE || hdr_sizes[tcode] == 0)
  189. return;
  190. while (size--)
  191. data[size] = swab32(data[size]);
  192. }
  193. #else
  194. /* Don't waste cycles on same sex byte swaps */
  195. #define packet_swab(w,x)
  196. #endif /* !LITTLE_ENDIAN */
  197. /***********************************
  198. * IEEE-1394 functionality section *
  199. ***********************************/
  200. static u8 get_phy_reg(struct ti_ohci *ohci, u8 addr)
  201. {
  202. int i;
  203. unsigned long flags;
  204. quadlet_t r;
  205. spin_lock_irqsave (&ohci->phy_reg_lock, flags);
  206. reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | 0x00008000);
  207. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  208. if (reg_read(ohci, OHCI1394_PhyControl) & 0x80000000)
  209. break;
  210. mdelay(1);
  211. }
  212. r = reg_read(ohci, OHCI1394_PhyControl);
  213. if (i >= OHCI_LOOP_COUNT)
  214. PRINT (KERN_ERR, "Get PHY Reg timeout [0x%08x/0x%08x/%d]",
  215. r, r & 0x80000000, i);
  216. spin_unlock_irqrestore (&ohci->phy_reg_lock, flags);
  217. return (r & 0x00ff0000) >> 16;
  218. }
  219. static void set_phy_reg(struct ti_ohci *ohci, u8 addr, u8 data)
  220. {
  221. int i;
  222. unsigned long flags;
  223. u32 r = 0;
  224. spin_lock_irqsave (&ohci->phy_reg_lock, flags);
  225. reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | data | 0x00004000);
  226. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  227. r = reg_read(ohci, OHCI1394_PhyControl);
  228. if (!(r & 0x00004000))
  229. break;
  230. mdelay(1);
  231. }
  232. if (i == OHCI_LOOP_COUNT)
  233. PRINT (KERN_ERR, "Set PHY Reg timeout [0x%08x/0x%08x/%d]",
  234. r, r & 0x00004000, i);
  235. spin_unlock_irqrestore (&ohci->phy_reg_lock, flags);
  236. return;
  237. }
  238. /* Or's our value into the current value */
  239. static void set_phy_reg_mask(struct ti_ohci *ohci, u8 addr, u8 data)
  240. {
  241. u8 old;
  242. old = get_phy_reg (ohci, addr);
  243. old |= data;
  244. set_phy_reg (ohci, addr, old);
  245. return;
  246. }
  247. static void handle_selfid(struct ti_ohci *ohci, struct hpsb_host *host,
  248. int phyid, int isroot)
  249. {
  250. quadlet_t *q = ohci->selfid_buf_cpu;
  251. quadlet_t self_id_count=reg_read(ohci, OHCI1394_SelfIDCount);
  252. size_t size;
  253. quadlet_t q0, q1;
  254. /* Check status of self-id reception */
  255. if (ohci->selfid_swap)
  256. q0 = le32_to_cpu(q[0]);
  257. else
  258. q0 = q[0];
  259. if ((self_id_count & 0x80000000) ||
  260. ((self_id_count & 0x00FF0000) != (q0 & 0x00FF0000))) {
  261. PRINT(KERN_ERR,
  262. "Error in reception of SelfID packets [0x%08x/0x%08x] (count: %d)",
  263. self_id_count, q0, ohci->self_id_errors);
  264. /* Tip by James Goodwin <jamesg@Filanet.com>:
  265. * We had an error, generate another bus reset in response. */
  266. if (ohci->self_id_errors<OHCI1394_MAX_SELF_ID_ERRORS) {
  267. set_phy_reg_mask (ohci, 1, 0x40);
  268. ohci->self_id_errors++;
  269. } else {
  270. PRINT(KERN_ERR,
  271. "Too many errors on SelfID error reception, giving up!");
  272. }
  273. return;
  274. }
  275. /* SelfID Ok, reset error counter. */
  276. ohci->self_id_errors = 0;
  277. size = ((self_id_count & 0x00001FFC) >> 2) - 1;
  278. q++;
  279. while (size > 0) {
  280. if (ohci->selfid_swap) {
  281. q0 = le32_to_cpu(q[0]);
  282. q1 = le32_to_cpu(q[1]);
  283. } else {
  284. q0 = q[0];
  285. q1 = q[1];
  286. }
  287. if (q0 == ~q1) {
  288. DBGMSG ("SelfID packet 0x%x received", q0);
  289. hpsb_selfid_received(host, cpu_to_be32(q0));
  290. if (((q0 & 0x3f000000) >> 24) == phyid)
  291. DBGMSG ("SelfID for this node is 0x%08x", q0);
  292. } else {
  293. PRINT(KERN_ERR,
  294. "SelfID is inconsistent [0x%08x/0x%08x]", q0, q1);
  295. }
  296. q += 2;
  297. size -= 2;
  298. }
  299. DBGMSG("SelfID complete");
  300. return;
  301. }
  302. static void ohci_soft_reset(struct ti_ohci *ohci) {
  303. int i;
  304. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
  305. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  306. if (!(reg_read(ohci, OHCI1394_HCControlSet) & OHCI1394_HCControl_softReset))
  307. break;
  308. mdelay(1);
  309. }
  310. DBGMSG ("Soft reset finished");
  311. }
  312. /* Generate the dma receive prgs and start the context */
  313. static void initialize_dma_rcv_ctx(struct dma_rcv_ctx *d, int generate_irq)
  314. {
  315. struct ti_ohci *ohci = (struct ti_ohci*)(d->ohci);
  316. int i;
  317. ohci1394_stop_context(ohci, d->ctrlClear, NULL);
  318. for (i=0; i<d->num_desc; i++) {
  319. u32 c;
  320. c = DMA_CTL_INPUT_MORE | DMA_CTL_UPDATE | DMA_CTL_BRANCH;
  321. if (generate_irq)
  322. c |= DMA_CTL_IRQ;
  323. d->prg_cpu[i]->control = cpu_to_le32(c | d->buf_size);
  324. /* End of descriptor list? */
  325. if (i + 1 < d->num_desc) {
  326. d->prg_cpu[i]->branchAddress =
  327. cpu_to_le32((d->prg_bus[i+1] & 0xfffffff0) | 0x1);
  328. } else {
  329. d->prg_cpu[i]->branchAddress =
  330. cpu_to_le32((d->prg_bus[0] & 0xfffffff0));
  331. }
  332. d->prg_cpu[i]->address = cpu_to_le32(d->buf_bus[i]);
  333. d->prg_cpu[i]->status = cpu_to_le32(d->buf_size);
  334. }
  335. d->buf_ind = 0;
  336. d->buf_offset = 0;
  337. if (d->type == DMA_CTX_ISO) {
  338. /* Clear contextControl */
  339. reg_write(ohci, d->ctrlClear, 0xffffffff);
  340. /* Set bufferFill, isochHeader, multichannel for IR context */
  341. reg_write(ohci, d->ctrlSet, 0xd0000000);
  342. /* Set the context match register to match on all tags */
  343. reg_write(ohci, d->ctxtMatch, 0xf0000000);
  344. /* Clear the multi channel mask high and low registers */
  345. reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear, 0xffffffff);
  346. reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear, 0xffffffff);
  347. /* Set up isoRecvIntMask to generate interrupts */
  348. reg_write(ohci, OHCI1394_IsoRecvIntMaskSet, 1 << d->ctx);
  349. }
  350. /* Tell the controller where the first AR program is */
  351. reg_write(ohci, d->cmdPtr, d->prg_bus[0] | 0x1);
  352. /* Run context */
  353. reg_write(ohci, d->ctrlSet, 0x00008000);
  354. DBGMSG("Receive DMA ctx=%d initialized", d->ctx);
  355. }
  356. /* Initialize the dma transmit context */
  357. static void initialize_dma_trm_ctx(struct dma_trm_ctx *d)
  358. {
  359. struct ti_ohci *ohci = (struct ti_ohci*)(d->ohci);
  360. /* Stop the context */
  361. ohci1394_stop_context(ohci, d->ctrlClear, NULL);
  362. d->prg_ind = 0;
  363. d->sent_ind = 0;
  364. d->free_prgs = d->num_desc;
  365. d->branchAddrPtr = NULL;
  366. INIT_LIST_HEAD(&d->fifo_list);
  367. INIT_LIST_HEAD(&d->pending_list);
  368. if (d->type == DMA_CTX_ISO) {
  369. /* enable interrupts */
  370. reg_write(ohci, OHCI1394_IsoXmitIntMaskSet, 1 << d->ctx);
  371. }
  372. DBGMSG("Transmit DMA ctx=%d initialized", d->ctx);
  373. }
  374. /* Count the number of available iso contexts */
  375. static int get_nb_iso_ctx(struct ti_ohci *ohci, int reg)
  376. {
  377. int i,ctx=0;
  378. u32 tmp;
  379. reg_write(ohci, reg, 0xffffffff);
  380. tmp = reg_read(ohci, reg);
  381. DBGMSG("Iso contexts reg: %08x implemented: %08x", reg, tmp);
  382. /* Count the number of contexts */
  383. for (i=0; i<32; i++) {
  384. if (tmp & 1) ctx++;
  385. tmp >>= 1;
  386. }
  387. return ctx;
  388. }
  389. /* Global initialization */
  390. static void ohci_initialize(struct ti_ohci *ohci)
  391. {
  392. char irq_buf[16];
  393. quadlet_t buf;
  394. int num_ports, i;
  395. spin_lock_init(&ohci->phy_reg_lock);
  396. spin_lock_init(&ohci->event_lock);
  397. /* Put some defaults to these undefined bus options */
  398. buf = reg_read(ohci, OHCI1394_BusOptions);
  399. buf |= 0x60000000; /* Enable CMC and ISC */
  400. if (!hpsb_disable_irm)
  401. buf |= 0x80000000; /* Enable IRMC */
  402. buf &= ~0x00ff0000; /* XXX: Set cyc_clk_acc to zero for now */
  403. buf &= ~0x18000000; /* Disable PMC and BMC */
  404. reg_write(ohci, OHCI1394_BusOptions, buf);
  405. /* Set the bus number */
  406. reg_write(ohci, OHCI1394_NodeID, 0x0000ffc0);
  407. /* Enable posted writes */
  408. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_postedWriteEnable);
  409. /* Clear link control register */
  410. reg_write(ohci, OHCI1394_LinkControlClear, 0xffffffff);
  411. /* Enable cycle timer and cycle master and set the IRM
  412. * contender bit in our self ID packets if appropriate. */
  413. reg_write(ohci, OHCI1394_LinkControlSet,
  414. OHCI1394_LinkControl_CycleTimerEnable |
  415. OHCI1394_LinkControl_CycleMaster);
  416. set_phy_reg_mask(ohci, 4, PHY_04_LCTRL |
  417. (hpsb_disable_irm ? 0 : PHY_04_CONTENDER));
  418. /* Set up self-id dma buffer */
  419. reg_write(ohci, OHCI1394_SelfIDBuffer, ohci->selfid_buf_bus);
  420. /* enable self-id and phys */
  421. reg_write(ohci, OHCI1394_LinkControlSet, OHCI1394_LinkControl_RcvSelfID |
  422. OHCI1394_LinkControl_RcvPhyPkt);
  423. /* Set the Config ROM mapping register */
  424. reg_write(ohci, OHCI1394_ConfigROMmap, ohci->csr_config_rom_bus);
  425. /* Now get our max packet size */
  426. ohci->max_packet_size =
  427. 1<<(((reg_read(ohci, OHCI1394_BusOptions)>>12)&0xf)+1);
  428. /* Don't accept phy packets into AR request context */
  429. reg_write(ohci, OHCI1394_LinkControlClear, 0x00000400);
  430. /* Clear the interrupt mask */
  431. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 0xffffffff);
  432. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 0xffffffff);
  433. /* Clear the interrupt mask */
  434. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 0xffffffff);
  435. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 0xffffffff);
  436. /* Initialize AR dma */
  437. initialize_dma_rcv_ctx(&ohci->ar_req_context, 0);
  438. initialize_dma_rcv_ctx(&ohci->ar_resp_context, 0);
  439. /* Initialize AT dma */
  440. initialize_dma_trm_ctx(&ohci->at_req_context);
  441. initialize_dma_trm_ctx(&ohci->at_resp_context);
  442. /* Initialize IR Legacy DMA */
  443. ohci->ir_legacy_channels = 0;
  444. initialize_dma_rcv_ctx(&ohci->ir_legacy_context, 1);
  445. DBGMSG("ISO receive legacy context activated");
  446. /*
  447. * Accept AT requests from all nodes. This probably
  448. * will have to be controlled from the subsystem
  449. * on a per node basis.
  450. */
  451. reg_write(ohci,OHCI1394_AsReqFilterHiSet, 0x80000000);
  452. /* Specify AT retries */
  453. reg_write(ohci, OHCI1394_ATRetries,
  454. OHCI1394_MAX_AT_REQ_RETRIES |
  455. (OHCI1394_MAX_AT_RESP_RETRIES<<4) |
  456. (OHCI1394_MAX_PHYS_RESP_RETRIES<<8));
  457. /* We don't want hardware swapping */
  458. reg_write(ohci, OHCI1394_HCControlClear, OHCI1394_HCControl_noByteSwap);
  459. /* Enable interrupts */
  460. reg_write(ohci, OHCI1394_IntMaskSet,
  461. OHCI1394_unrecoverableError |
  462. OHCI1394_masterIntEnable |
  463. OHCI1394_busReset |
  464. OHCI1394_selfIDComplete |
  465. OHCI1394_RSPkt |
  466. OHCI1394_RQPkt |
  467. OHCI1394_respTxComplete |
  468. OHCI1394_reqTxComplete |
  469. OHCI1394_isochRx |
  470. OHCI1394_isochTx |
  471. OHCI1394_cycleInconsistent);
  472. /* Enable link */
  473. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_linkEnable);
  474. buf = reg_read(ohci, OHCI1394_Version);
  475. #ifndef __sparc__
  476. sprintf (irq_buf, "%d", ohci->dev->irq);
  477. #else
  478. sprintf (irq_buf, "%s", __irq_itoa(ohci->dev->irq));
  479. #endif
  480. PRINT(KERN_INFO, "OHCI-1394 %d.%d (PCI): IRQ=[%s] "
  481. "MMIO=[%lx-%lx] Max Packet=[%d]",
  482. ((((buf) >> 16) & 0xf) + (((buf) >> 20) & 0xf) * 10),
  483. ((((buf) >> 4) & 0xf) + ((buf) & 0xf) * 10), irq_buf,
  484. pci_resource_start(ohci->dev, 0),
  485. pci_resource_start(ohci->dev, 0) + OHCI1394_REGISTER_SIZE - 1,
  486. ohci->max_packet_size);
  487. /* Check all of our ports to make sure that if anything is
  488. * connected, we enable that port. */
  489. num_ports = get_phy_reg(ohci, 2) & 0xf;
  490. for (i = 0; i < num_ports; i++) {
  491. unsigned int status;
  492. set_phy_reg(ohci, 7, i);
  493. status = get_phy_reg(ohci, 8);
  494. if (status & 0x20)
  495. set_phy_reg(ohci, 8, status & ~1);
  496. }
  497. /* Serial EEPROM Sanity check. */
  498. if ((ohci->max_packet_size < 512) ||
  499. (ohci->max_packet_size > 4096)) {
  500. /* Serial EEPROM contents are suspect, set a sane max packet
  501. * size and print the raw contents for bug reports if verbose
  502. * debug is enabled. */
  503. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  504. int i;
  505. #endif
  506. PRINT(KERN_DEBUG, "Serial EEPROM has suspicious values, "
  507. "attempting to setting max_packet_size to 512 bytes");
  508. reg_write(ohci, OHCI1394_BusOptions,
  509. (reg_read(ohci, OHCI1394_BusOptions) & 0xf007) | 0x8002);
  510. ohci->max_packet_size = 512;
  511. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  512. PRINT(KERN_DEBUG, " EEPROM Present: %d",
  513. (reg_read(ohci, OHCI1394_Version) >> 24) & 0x1);
  514. reg_write(ohci, OHCI1394_GUID_ROM, 0x80000000);
  515. for (i = 0;
  516. ((i < 1000) &&
  517. (reg_read(ohci, OHCI1394_GUID_ROM) & 0x80000000)); i++)
  518. udelay(10);
  519. for (i = 0; i < 0x20; i++) {
  520. reg_write(ohci, OHCI1394_GUID_ROM, 0x02000000);
  521. PRINT(KERN_DEBUG, " EEPROM %02x: %02x", i,
  522. (reg_read(ohci, OHCI1394_GUID_ROM) >> 16) & 0xff);
  523. }
  524. #endif
  525. }
  526. }
  527. /*
  528. * Insert a packet in the DMA fifo and generate the DMA prg
  529. * FIXME: rewrite the program in order to accept packets crossing
  530. * page boundaries.
  531. * check also that a single dma descriptor doesn't cross a
  532. * page boundary.
  533. */
  534. static void insert_packet(struct ti_ohci *ohci,
  535. struct dma_trm_ctx *d, struct hpsb_packet *packet)
  536. {
  537. u32 cycleTimer;
  538. int idx = d->prg_ind;
  539. DBGMSG("Inserting packet for node " NODE_BUS_FMT
  540. ", tlabel=%d, tcode=0x%x, speed=%d",
  541. NODE_BUS_ARGS(ohci->host, packet->node_id), packet->tlabel,
  542. packet->tcode, packet->speed_code);
  543. d->prg_cpu[idx]->begin.address = 0;
  544. d->prg_cpu[idx]->begin.branchAddress = 0;
  545. if (d->type == DMA_CTX_ASYNC_RESP) {
  546. /*
  547. * For response packets, we need to put a timeout value in
  548. * the 16 lower bits of the status... let's try 1 sec timeout
  549. */
  550. cycleTimer = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  551. d->prg_cpu[idx]->begin.status = cpu_to_le32(
  552. (((((cycleTimer>>25)&0x7)+1)&0x7)<<13) |
  553. ((cycleTimer&0x01fff000)>>12));
  554. DBGMSG("cycleTimer: %08x timeStamp: %08x",
  555. cycleTimer, d->prg_cpu[idx]->begin.status);
  556. } else
  557. d->prg_cpu[idx]->begin.status = 0;
  558. if ( (packet->type == hpsb_async) || (packet->type == hpsb_raw) ) {
  559. if (packet->type == hpsb_raw) {
  560. d->prg_cpu[idx]->data[0] = cpu_to_le32(OHCI1394_TCODE_PHY<<4);
  561. d->prg_cpu[idx]->data[1] = cpu_to_le32(packet->header[0]);
  562. d->prg_cpu[idx]->data[2] = cpu_to_le32(packet->header[1]);
  563. } else {
  564. d->prg_cpu[idx]->data[0] = packet->speed_code<<16 |
  565. (packet->header[0] & 0xFFFF);
  566. if (packet->tcode == TCODE_ISO_DATA) {
  567. /* Sending an async stream packet */
  568. d->prg_cpu[idx]->data[1] = packet->header[0] & 0xFFFF0000;
  569. } else {
  570. /* Sending a normal async request or response */
  571. d->prg_cpu[idx]->data[1] =
  572. (packet->header[1] & 0xFFFF) |
  573. (packet->header[0] & 0xFFFF0000);
  574. d->prg_cpu[idx]->data[2] = packet->header[2];
  575. d->prg_cpu[idx]->data[3] = packet->header[3];
  576. }
  577. packet_swab(d->prg_cpu[idx]->data, packet->tcode);
  578. }
  579. if (packet->data_size) { /* block transmit */
  580. if (packet->tcode == TCODE_STREAM_DATA){
  581. d->prg_cpu[idx]->begin.control =
  582. cpu_to_le32(DMA_CTL_OUTPUT_MORE |
  583. DMA_CTL_IMMEDIATE | 0x8);
  584. } else {
  585. d->prg_cpu[idx]->begin.control =
  586. cpu_to_le32(DMA_CTL_OUTPUT_MORE |
  587. DMA_CTL_IMMEDIATE | 0x10);
  588. }
  589. d->prg_cpu[idx]->end.control =
  590. cpu_to_le32(DMA_CTL_OUTPUT_LAST |
  591. DMA_CTL_IRQ |
  592. DMA_CTL_BRANCH |
  593. packet->data_size);
  594. /*
  595. * Check that the packet data buffer
  596. * does not cross a page boundary.
  597. *
  598. * XXX Fix this some day. eth1394 seems to trigger
  599. * it, but ignoring it doesn't seem to cause a
  600. * problem.
  601. */
  602. #if 0
  603. if (cross_bound((unsigned long)packet->data,
  604. packet->data_size)>0) {
  605. /* FIXME: do something about it */
  606. PRINT(KERN_ERR,
  607. "%s: packet data addr: %p size %Zd bytes "
  608. "cross page boundary", __FUNCTION__,
  609. packet->data, packet->data_size);
  610. }
  611. #endif
  612. d->prg_cpu[idx]->end.address = cpu_to_le32(
  613. pci_map_single(ohci->dev, packet->data,
  614. packet->data_size,
  615. PCI_DMA_TODEVICE));
  616. OHCI_DMA_ALLOC("single, block transmit packet");
  617. d->prg_cpu[idx]->end.branchAddress = 0;
  618. d->prg_cpu[idx]->end.status = 0;
  619. if (d->branchAddrPtr)
  620. *(d->branchAddrPtr) =
  621. cpu_to_le32(d->prg_bus[idx] | 0x3);
  622. d->branchAddrPtr =
  623. &(d->prg_cpu[idx]->end.branchAddress);
  624. } else { /* quadlet transmit */
  625. if (packet->type == hpsb_raw)
  626. d->prg_cpu[idx]->begin.control =
  627. cpu_to_le32(DMA_CTL_OUTPUT_LAST |
  628. DMA_CTL_IMMEDIATE |
  629. DMA_CTL_IRQ |
  630. DMA_CTL_BRANCH |
  631. (packet->header_size + 4));
  632. else
  633. d->prg_cpu[idx]->begin.control =
  634. cpu_to_le32(DMA_CTL_OUTPUT_LAST |
  635. DMA_CTL_IMMEDIATE |
  636. DMA_CTL_IRQ |
  637. DMA_CTL_BRANCH |
  638. packet->header_size);
  639. if (d->branchAddrPtr)
  640. *(d->branchAddrPtr) =
  641. cpu_to_le32(d->prg_bus[idx] | 0x2);
  642. d->branchAddrPtr =
  643. &(d->prg_cpu[idx]->begin.branchAddress);
  644. }
  645. } else { /* iso packet */
  646. d->prg_cpu[idx]->data[0] = packet->speed_code<<16 |
  647. (packet->header[0] & 0xFFFF);
  648. d->prg_cpu[idx]->data[1] = packet->header[0] & 0xFFFF0000;
  649. packet_swab(d->prg_cpu[idx]->data, packet->tcode);
  650. d->prg_cpu[idx]->begin.control =
  651. cpu_to_le32(DMA_CTL_OUTPUT_MORE |
  652. DMA_CTL_IMMEDIATE | 0x8);
  653. d->prg_cpu[idx]->end.control =
  654. cpu_to_le32(DMA_CTL_OUTPUT_LAST |
  655. DMA_CTL_UPDATE |
  656. DMA_CTL_IRQ |
  657. DMA_CTL_BRANCH |
  658. packet->data_size);
  659. d->prg_cpu[idx]->end.address = cpu_to_le32(
  660. pci_map_single(ohci->dev, packet->data,
  661. packet->data_size, PCI_DMA_TODEVICE));
  662. OHCI_DMA_ALLOC("single, iso transmit packet");
  663. d->prg_cpu[idx]->end.branchAddress = 0;
  664. d->prg_cpu[idx]->end.status = 0;
  665. DBGMSG("Iso xmit context info: header[%08x %08x]\n"
  666. " begin=%08x %08x %08x %08x\n"
  667. " %08x %08x %08x %08x\n"
  668. " end =%08x %08x %08x %08x",
  669. d->prg_cpu[idx]->data[0], d->prg_cpu[idx]->data[1],
  670. d->prg_cpu[idx]->begin.control,
  671. d->prg_cpu[idx]->begin.address,
  672. d->prg_cpu[idx]->begin.branchAddress,
  673. d->prg_cpu[idx]->begin.status,
  674. d->prg_cpu[idx]->data[0],
  675. d->prg_cpu[idx]->data[1],
  676. d->prg_cpu[idx]->data[2],
  677. d->prg_cpu[idx]->data[3],
  678. d->prg_cpu[idx]->end.control,
  679. d->prg_cpu[idx]->end.address,
  680. d->prg_cpu[idx]->end.branchAddress,
  681. d->prg_cpu[idx]->end.status);
  682. if (d->branchAddrPtr)
  683. *(d->branchAddrPtr) = cpu_to_le32(d->prg_bus[idx] | 0x3);
  684. d->branchAddrPtr = &(d->prg_cpu[idx]->end.branchAddress);
  685. }
  686. d->free_prgs--;
  687. /* queue the packet in the appropriate context queue */
  688. list_add_tail(&packet->driver_list, &d->fifo_list);
  689. d->prg_ind = (d->prg_ind + 1) % d->num_desc;
  690. }
  691. /*
  692. * This function fills the FIFO with the (eventual) pending packets
  693. * and runs or wakes up the DMA prg if necessary.
  694. *
  695. * The function MUST be called with the d->lock held.
  696. */
  697. static void dma_trm_flush(struct ti_ohci *ohci, struct dma_trm_ctx *d)
  698. {
  699. struct hpsb_packet *packet, *ptmp;
  700. int idx = d->prg_ind;
  701. int z = 0;
  702. /* insert the packets into the dma fifo */
  703. list_for_each_entry_safe(packet, ptmp, &d->pending_list, driver_list) {
  704. if (!d->free_prgs)
  705. break;
  706. /* For the first packet only */
  707. if (!z)
  708. z = (packet->data_size) ? 3 : 2;
  709. /* Insert the packet */
  710. list_del_init(&packet->driver_list);
  711. insert_packet(ohci, d, packet);
  712. }
  713. /* Nothing must have been done, either no free_prgs or no packets */
  714. if (z == 0)
  715. return;
  716. /* Is the context running ? (should be unless it is
  717. the first packet to be sent in this context) */
  718. if (!(reg_read(ohci, d->ctrlSet) & 0x8000)) {
  719. u32 nodeId = reg_read(ohci, OHCI1394_NodeID);
  720. DBGMSG("Starting transmit DMA ctx=%d",d->ctx);
  721. reg_write(ohci, d->cmdPtr, d->prg_bus[idx] | z);
  722. /* Check that the node id is valid, and not 63 */
  723. if (!(nodeId & 0x80000000) || (nodeId & 0x3f) == 63)
  724. PRINT(KERN_ERR, "Running dma failed because Node ID is not valid");
  725. else
  726. reg_write(ohci, d->ctrlSet, 0x8000);
  727. } else {
  728. /* Wake up the dma context if necessary */
  729. if (!(reg_read(ohci, d->ctrlSet) & 0x400))
  730. DBGMSG("Waking transmit DMA ctx=%d",d->ctx);
  731. /* do this always, to avoid race condition */
  732. reg_write(ohci, d->ctrlSet, 0x1000);
  733. }
  734. return;
  735. }
  736. /* Transmission of an async or iso packet */
  737. static int ohci_transmit(struct hpsb_host *host, struct hpsb_packet *packet)
  738. {
  739. struct ti_ohci *ohci = host->hostdata;
  740. struct dma_trm_ctx *d;
  741. unsigned long flags;
  742. if (packet->data_size > ohci->max_packet_size) {
  743. PRINT(KERN_ERR,
  744. "Transmit packet size %Zd is too big",
  745. packet->data_size);
  746. return -EOVERFLOW;
  747. }
  748. /* Decide whether we have an iso, a request, or a response packet */
  749. if (packet->type == hpsb_raw)
  750. d = &ohci->at_req_context;
  751. else if ((packet->tcode == TCODE_ISO_DATA) && (packet->type == hpsb_iso)) {
  752. /* The legacy IT DMA context is initialized on first
  753. * use. However, the alloc cannot be run from
  754. * interrupt context, so we bail out if that is the
  755. * case. I don't see anyone sending ISO packets from
  756. * interrupt context anyway... */
  757. if (ohci->it_legacy_context.ohci == NULL) {
  758. if (in_interrupt()) {
  759. PRINT(KERN_ERR,
  760. "legacy IT context cannot be initialized during interrupt");
  761. return -EINVAL;
  762. }
  763. if (alloc_dma_trm_ctx(ohci, &ohci->it_legacy_context,
  764. DMA_CTX_ISO, 0, IT_NUM_DESC,
  765. OHCI1394_IsoXmitContextBase) < 0) {
  766. PRINT(KERN_ERR,
  767. "error initializing legacy IT context");
  768. return -ENOMEM;
  769. }
  770. initialize_dma_trm_ctx(&ohci->it_legacy_context);
  771. }
  772. d = &ohci->it_legacy_context;
  773. } else if ((packet->tcode & 0x02) && (packet->tcode != TCODE_ISO_DATA))
  774. d = &ohci->at_resp_context;
  775. else
  776. d = &ohci->at_req_context;
  777. spin_lock_irqsave(&d->lock,flags);
  778. list_add_tail(&packet->driver_list, &d->pending_list);
  779. dma_trm_flush(ohci, d);
  780. spin_unlock_irqrestore(&d->lock,flags);
  781. return 0;
  782. }
  783. static int ohci_devctl(struct hpsb_host *host, enum devctl_cmd cmd, int arg)
  784. {
  785. struct ti_ohci *ohci = host->hostdata;
  786. int retval = 0;
  787. unsigned long flags;
  788. int phy_reg;
  789. switch (cmd) {
  790. case RESET_BUS:
  791. switch (arg) {
  792. case SHORT_RESET:
  793. phy_reg = get_phy_reg(ohci, 5);
  794. phy_reg |= 0x40;
  795. set_phy_reg(ohci, 5, phy_reg); /* set ISBR */
  796. break;
  797. case LONG_RESET:
  798. phy_reg = get_phy_reg(ohci, 1);
  799. phy_reg |= 0x40;
  800. set_phy_reg(ohci, 1, phy_reg); /* set IBR */
  801. break;
  802. case SHORT_RESET_NO_FORCE_ROOT:
  803. phy_reg = get_phy_reg(ohci, 1);
  804. if (phy_reg & 0x80) {
  805. phy_reg &= ~0x80;
  806. set_phy_reg(ohci, 1, phy_reg); /* clear RHB */
  807. }
  808. phy_reg = get_phy_reg(ohci, 5);
  809. phy_reg |= 0x40;
  810. set_phy_reg(ohci, 5, phy_reg); /* set ISBR */
  811. break;
  812. case LONG_RESET_NO_FORCE_ROOT:
  813. phy_reg = get_phy_reg(ohci, 1);
  814. phy_reg &= ~0x80;
  815. phy_reg |= 0x40;
  816. set_phy_reg(ohci, 1, phy_reg); /* clear RHB, set IBR */
  817. break;
  818. case SHORT_RESET_FORCE_ROOT:
  819. phy_reg = get_phy_reg(ohci, 1);
  820. if (!(phy_reg & 0x80)) {
  821. phy_reg |= 0x80;
  822. set_phy_reg(ohci, 1, phy_reg); /* set RHB */
  823. }
  824. phy_reg = get_phy_reg(ohci, 5);
  825. phy_reg |= 0x40;
  826. set_phy_reg(ohci, 5, phy_reg); /* set ISBR */
  827. break;
  828. case LONG_RESET_FORCE_ROOT:
  829. phy_reg = get_phy_reg(ohci, 1);
  830. phy_reg |= 0xc0;
  831. set_phy_reg(ohci, 1, phy_reg); /* set RHB and IBR */
  832. break;
  833. default:
  834. retval = -1;
  835. }
  836. break;
  837. case GET_CYCLE_COUNTER:
  838. retval = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
  839. break;
  840. case SET_CYCLE_COUNTER:
  841. reg_write(ohci, OHCI1394_IsochronousCycleTimer, arg);
  842. break;
  843. case SET_BUS_ID:
  844. PRINT(KERN_ERR, "devctl command SET_BUS_ID err");
  845. break;
  846. case ACT_CYCLE_MASTER:
  847. if (arg) {
  848. /* check if we are root and other nodes are present */
  849. u32 nodeId = reg_read(ohci, OHCI1394_NodeID);
  850. if ((nodeId & (1<<30)) && (nodeId & 0x3f)) {
  851. /*
  852. * enable cycleTimer, cycleMaster
  853. */
  854. DBGMSG("Cycle master enabled");
  855. reg_write(ohci, OHCI1394_LinkControlSet,
  856. OHCI1394_LinkControl_CycleTimerEnable |
  857. OHCI1394_LinkControl_CycleMaster);
  858. }
  859. } else {
  860. /* disable cycleTimer, cycleMaster, cycleSource */
  861. reg_write(ohci, OHCI1394_LinkControlClear,
  862. OHCI1394_LinkControl_CycleTimerEnable |
  863. OHCI1394_LinkControl_CycleMaster |
  864. OHCI1394_LinkControl_CycleSource);
  865. }
  866. break;
  867. case CANCEL_REQUESTS:
  868. DBGMSG("Cancel request received");
  869. dma_trm_reset(&ohci->at_req_context);
  870. dma_trm_reset(&ohci->at_resp_context);
  871. break;
  872. case ISO_LISTEN_CHANNEL:
  873. {
  874. u64 mask;
  875. if (arg<0 || arg>63) {
  876. PRINT(KERN_ERR,
  877. "%s: IS0 listen channel %d is out of range",
  878. __FUNCTION__, arg);
  879. return -EFAULT;
  880. }
  881. mask = (u64)0x1<<arg;
  882. spin_lock_irqsave(&ohci->IR_channel_lock, flags);
  883. if (ohci->ISO_channel_usage & mask) {
  884. PRINT(KERN_ERR,
  885. "%s: IS0 listen channel %d is already used",
  886. __FUNCTION__, arg);
  887. spin_unlock_irqrestore(&ohci->IR_channel_lock, flags);
  888. return -EFAULT;
  889. }
  890. ohci->ISO_channel_usage |= mask;
  891. ohci->ir_legacy_channels |= mask;
  892. if (arg>31)
  893. reg_write(ohci, OHCI1394_IRMultiChanMaskHiSet,
  894. 1<<(arg-32));
  895. else
  896. reg_write(ohci, OHCI1394_IRMultiChanMaskLoSet,
  897. 1<<arg);
  898. spin_unlock_irqrestore(&ohci->IR_channel_lock, flags);
  899. DBGMSG("Listening enabled on channel %d", arg);
  900. break;
  901. }
  902. case ISO_UNLISTEN_CHANNEL:
  903. {
  904. u64 mask;
  905. if (arg<0 || arg>63) {
  906. PRINT(KERN_ERR,
  907. "%s: IS0 unlisten channel %d is out of range",
  908. __FUNCTION__, arg);
  909. return -EFAULT;
  910. }
  911. mask = (u64)0x1<<arg;
  912. spin_lock_irqsave(&ohci->IR_channel_lock, flags);
  913. if (!(ohci->ISO_channel_usage & mask)) {
  914. PRINT(KERN_ERR,
  915. "%s: IS0 unlisten channel %d is not used",
  916. __FUNCTION__, arg);
  917. spin_unlock_irqrestore(&ohci->IR_channel_lock, flags);
  918. return -EFAULT;
  919. }
  920. ohci->ISO_channel_usage &= ~mask;
  921. ohci->ir_legacy_channels &= ~mask;
  922. if (arg>31)
  923. reg_write(ohci, OHCI1394_IRMultiChanMaskHiClear,
  924. 1<<(arg-32));
  925. else
  926. reg_write(ohci, OHCI1394_IRMultiChanMaskLoClear,
  927. 1<<arg);
  928. spin_unlock_irqrestore(&ohci->IR_channel_lock, flags);
  929. DBGMSG("Listening disabled on channel %d", arg);
  930. break;
  931. }
  932. default:
  933. PRINT_G(KERN_ERR, "ohci_devctl cmd %d not implemented yet",
  934. cmd);
  935. break;
  936. }
  937. return retval;
  938. }
  939. /***********************************
  940. * rawiso ISO reception *
  941. ***********************************/
  942. /*
  943. We use either buffer-fill or packet-per-buffer DMA mode. The DMA
  944. buffer is split into "blocks" (regions described by one DMA
  945. descriptor). Each block must be one page or less in size, and
  946. must not cross a page boundary.
  947. There is one little wrinkle with buffer-fill mode: a packet that
  948. starts in the final block may wrap around into the first block. But
  949. the user API expects all packets to be contiguous. Our solution is
  950. to keep the very last page of the DMA buffer in reserve - if a
  951. packet spans the gap, we copy its tail into this page.
  952. */
  953. struct ohci_iso_recv {
  954. struct ti_ohci *ohci;
  955. struct ohci1394_iso_tasklet task;
  956. int task_active;
  957. enum { BUFFER_FILL_MODE = 0,
  958. PACKET_PER_BUFFER_MODE = 1 } dma_mode;
  959. /* memory and PCI mapping for the DMA descriptors */
  960. struct dma_prog_region prog;
  961. struct dma_cmd *block; /* = (struct dma_cmd*) prog.virt */
  962. /* how many DMA blocks fit in the buffer */
  963. unsigned int nblocks;
  964. /* stride of DMA blocks */
  965. unsigned int buf_stride;
  966. /* number of blocks to batch between interrupts */
  967. int block_irq_interval;
  968. /* block that DMA will finish next */
  969. int block_dma;
  970. /* (buffer-fill only) block that the reader will release next */
  971. int block_reader;
  972. /* (buffer-fill only) bytes of buffer the reader has released,
  973. less than one block */
  974. int released_bytes;
  975. /* (buffer-fill only) buffer offset at which the next packet will appear */
  976. int dma_offset;
  977. /* OHCI DMA context control registers */
  978. u32 ContextControlSet;
  979. u32 ContextControlClear;
  980. u32 CommandPtr;
  981. u32 ContextMatch;
  982. };
  983. static void ohci_iso_recv_task(unsigned long data);
  984. static void ohci_iso_recv_stop(struct hpsb_iso *iso);
  985. static void ohci_iso_recv_shutdown(struct hpsb_iso *iso);
  986. static int ohci_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync);
  987. static void ohci_iso_recv_program(struct hpsb_iso *iso);
  988. static int ohci_iso_recv_init(struct hpsb_iso *iso)
  989. {
  990. struct ti_ohci *ohci = iso->host->hostdata;
  991. struct ohci_iso_recv *recv;
  992. int ctx;
  993. int ret = -ENOMEM;
  994. recv = kmalloc(sizeof(*recv), SLAB_KERNEL);
  995. if (!recv)
  996. return -ENOMEM;
  997. iso->hostdata = recv;
  998. recv->ohci = ohci;
  999. recv->task_active = 0;
  1000. dma_prog_region_init(&recv->prog);
  1001. recv->block = NULL;
  1002. /* use buffer-fill mode, unless irq_interval is 1
  1003. (note: multichannel requires buffer-fill) */
  1004. if (((iso->irq_interval == 1 && iso->dma_mode == HPSB_ISO_DMA_OLD_ABI) ||
  1005. iso->dma_mode == HPSB_ISO_DMA_PACKET_PER_BUFFER) && iso->channel != -1) {
  1006. recv->dma_mode = PACKET_PER_BUFFER_MODE;
  1007. } else {
  1008. recv->dma_mode = BUFFER_FILL_MODE;
  1009. }
  1010. /* set nblocks, buf_stride, block_irq_interval */
  1011. if (recv->dma_mode == BUFFER_FILL_MODE) {
  1012. recv->buf_stride = PAGE_SIZE;
  1013. /* one block per page of data in the DMA buffer, minus the final guard page */
  1014. recv->nblocks = iso->buf_size/PAGE_SIZE - 1;
  1015. if (recv->nblocks < 3) {
  1016. DBGMSG("ohci_iso_recv_init: DMA buffer too small");
  1017. goto err;
  1018. }
  1019. /* iso->irq_interval is in packets - translate that to blocks */
  1020. if (iso->irq_interval == 1)
  1021. recv->block_irq_interval = 1;
  1022. else
  1023. recv->block_irq_interval = iso->irq_interval *
  1024. ((recv->nblocks+1)/iso->buf_packets);
  1025. if (recv->block_irq_interval*4 > recv->nblocks)
  1026. recv->block_irq_interval = recv->nblocks/4;
  1027. if (recv->block_irq_interval < 1)
  1028. recv->block_irq_interval = 1;
  1029. } else {
  1030. int max_packet_size;
  1031. recv->nblocks = iso->buf_packets;
  1032. recv->block_irq_interval = iso->irq_interval;
  1033. if (recv->block_irq_interval * 4 > iso->buf_packets)
  1034. recv->block_irq_interval = iso->buf_packets / 4;
  1035. if (recv->block_irq_interval < 1)
  1036. recv->block_irq_interval = 1;
  1037. /* choose a buffer stride */
  1038. /* must be a power of 2, and <= PAGE_SIZE */
  1039. max_packet_size = iso->buf_size / iso->buf_packets;
  1040. for (recv->buf_stride = 8; recv->buf_stride < max_packet_size;
  1041. recv->buf_stride *= 2);
  1042. if (recv->buf_stride*iso->buf_packets > iso->buf_size ||
  1043. recv->buf_stride > PAGE_SIZE) {
  1044. /* this shouldn't happen, but anyway... */
  1045. DBGMSG("ohci_iso_recv_init: problem choosing a buffer stride");
  1046. goto err;
  1047. }
  1048. }
  1049. recv->block_reader = 0;
  1050. recv->released_bytes = 0;
  1051. recv->block_dma = 0;
  1052. recv->dma_offset = 0;
  1053. /* size of DMA program = one descriptor per block */
  1054. if (dma_prog_region_alloc(&recv->prog,
  1055. sizeof(struct dma_cmd) * recv->nblocks,
  1056. recv->ohci->dev))
  1057. goto err;
  1058. recv->block = (struct dma_cmd*) recv->prog.kvirt;
  1059. ohci1394_init_iso_tasklet(&recv->task,
  1060. iso->channel == -1 ? OHCI_ISO_MULTICHANNEL_RECEIVE :
  1061. OHCI_ISO_RECEIVE,
  1062. ohci_iso_recv_task, (unsigned long) iso);
  1063. if (ohci1394_register_iso_tasklet(recv->ohci, &recv->task) < 0)
  1064. goto err;
  1065. recv->task_active = 1;
  1066. /* recv context registers are spaced 32 bytes apart */
  1067. ctx = recv->task.context;
  1068. recv->ContextControlSet = OHCI1394_IsoRcvContextControlSet + 32 * ctx;
  1069. recv->ContextControlClear = OHCI1394_IsoRcvContextControlClear + 32 * ctx;
  1070. recv->CommandPtr = OHCI1394_IsoRcvCommandPtr + 32 * ctx;
  1071. recv->ContextMatch = OHCI1394_IsoRcvContextMatch + 32 * ctx;
  1072. if (iso->channel == -1) {
  1073. /* clear multi-channel selection mask */
  1074. reg_write(recv->ohci, OHCI1394_IRMultiChanMaskHiClear, 0xFFFFFFFF);
  1075. reg_write(recv->ohci, OHCI1394_IRMultiChanMaskLoClear, 0xFFFFFFFF);
  1076. }
  1077. /* write the DMA program */
  1078. ohci_iso_recv_program(iso);
  1079. DBGMSG("ohci_iso_recv_init: %s mode, DMA buffer is %lu pages"
  1080. " (%u bytes), using %u blocks, buf_stride %u, block_irq_interval %d",
  1081. recv->dma_mode == BUFFER_FILL_MODE ?
  1082. "buffer-fill" : "packet-per-buffer",
  1083. iso->buf_size/PAGE_SIZE, iso->buf_size,
  1084. recv->nblocks, recv->buf_stride, recv->block_irq_interval);
  1085. return 0;
  1086. err:
  1087. ohci_iso_recv_shutdown(iso);
  1088. return ret;
  1089. }
  1090. static void ohci_iso_recv_stop(struct hpsb_iso *iso)
  1091. {
  1092. struct ohci_iso_recv *recv = iso->hostdata;
  1093. /* disable interrupts */
  1094. reg_write(recv->ohci, OHCI1394_IsoRecvIntMaskClear, 1 << recv->task.context);
  1095. /* halt DMA */
  1096. ohci1394_stop_context(recv->ohci, recv->ContextControlClear, NULL);
  1097. }
  1098. static void ohci_iso_recv_shutdown(struct hpsb_iso *iso)
  1099. {
  1100. struct ohci_iso_recv *recv = iso->hostdata;
  1101. if (recv->task_active) {
  1102. ohci_iso_recv_stop(iso);
  1103. ohci1394_unregister_iso_tasklet(recv->ohci, &recv->task);
  1104. recv->task_active = 0;
  1105. }
  1106. dma_prog_region_free(&recv->prog);
  1107. kfree(recv);
  1108. iso->hostdata = NULL;
  1109. }
  1110. /* set up a "gapped" ring buffer DMA program */
  1111. static void ohci_iso_recv_program(struct hpsb_iso *iso)
  1112. {
  1113. struct ohci_iso_recv *recv = iso->hostdata;
  1114. int blk;
  1115. /* address of 'branch' field in previous DMA descriptor */
  1116. u32 *prev_branch = NULL;
  1117. for (blk = 0; blk < recv->nblocks; blk++) {
  1118. u32 control;
  1119. /* the DMA descriptor */
  1120. struct dma_cmd *cmd = &recv->block[blk];
  1121. /* offset of the DMA descriptor relative to the DMA prog buffer */
  1122. unsigned long prog_offset = blk * sizeof(struct dma_cmd);
  1123. /* offset of this packet's data within the DMA buffer */
  1124. unsigned long buf_offset = blk * recv->buf_stride;
  1125. if (recv->dma_mode == BUFFER_FILL_MODE) {
  1126. control = 2 << 28; /* INPUT_MORE */
  1127. } else {
  1128. control = 3 << 28; /* INPUT_LAST */
  1129. }
  1130. control |= 8 << 24; /* s = 1, update xferStatus and resCount */
  1131. /* interrupt on last block, and at intervals */
  1132. if (blk == recv->nblocks-1 || (blk % recv->block_irq_interval) == 0) {
  1133. control |= 3 << 20; /* want interrupt */
  1134. }
  1135. control |= 3 << 18; /* enable branch to address */
  1136. control |= recv->buf_stride;
  1137. cmd->control = cpu_to_le32(control);
  1138. cmd->address = cpu_to_le32(dma_region_offset_to_bus(&iso->data_buf, buf_offset));
  1139. cmd->branchAddress = 0; /* filled in on next loop */
  1140. cmd->status = cpu_to_le32(recv->buf_stride);
  1141. /* link the previous descriptor to this one */
  1142. if (prev_branch) {
  1143. *prev_branch = cpu_to_le32(dma_prog_region_offset_to_bus(&recv->prog, prog_offset) | 1);
  1144. }
  1145. prev_branch = &cmd->branchAddress;
  1146. }
  1147. /* the final descriptor's branch address and Z should be left at 0 */
  1148. }
  1149. /* listen or unlisten to a specific channel (multi-channel mode only) */
  1150. static void ohci_iso_recv_change_channel(struct hpsb_iso *iso, unsigned char channel, int listen)
  1151. {
  1152. struct ohci_iso_recv *recv = iso->hostdata;
  1153. int reg, i;
  1154. if (channel < 32) {
  1155. reg = listen ? OHCI1394_IRMultiChanMaskLoSet : OHCI1394_IRMultiChanMaskLoClear;
  1156. i = channel;
  1157. } else {
  1158. reg = listen ? OHCI1394_IRMultiChanMaskHiSet : OHCI1394_IRMultiChanMaskHiClear;
  1159. i = channel - 32;
  1160. }
  1161. reg_write(recv->ohci, reg, (1 << i));
  1162. /* issue a dummy read to force all PCI writes to be posted immediately */
  1163. mb();
  1164. reg_read(recv->ohci, OHCI1394_IsochronousCycleTimer);
  1165. }
  1166. static void ohci_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
  1167. {
  1168. struct ohci_iso_recv *recv = iso->hostdata;
  1169. int i;
  1170. for (i = 0; i < 64; i++) {
  1171. if (mask & (1ULL << i)) {
  1172. if (i < 32)
  1173. reg_write(recv->ohci, OHCI1394_IRMultiChanMaskLoSet, (1 << i));
  1174. else
  1175. reg_write(recv->ohci, OHCI1394_IRMultiChanMaskHiSet, (1 << (i-32)));
  1176. } else {
  1177. if (i < 32)
  1178. reg_write(recv->ohci, OHCI1394_IRMultiChanMaskLoClear, (1 << i));
  1179. else
  1180. reg_write(recv->ohci, OHCI1394_IRMultiChanMaskHiClear, (1 << (i-32)));
  1181. }
  1182. }
  1183. /* issue a dummy read to force all PCI writes to be posted immediately */
  1184. mb();
  1185. reg_read(recv->ohci, OHCI1394_IsochronousCycleTimer);
  1186. }
  1187. static int ohci_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
  1188. {
  1189. struct ohci_iso_recv *recv = iso->hostdata;
  1190. struct ti_ohci *ohci = recv->ohci;
  1191. u32 command, contextMatch;
  1192. reg_write(recv->ohci, recv->ContextControlClear, 0xFFFFFFFF);
  1193. wmb();
  1194. /* always keep ISO headers */
  1195. command = (1 << 30);
  1196. if (recv->dma_mode == BUFFER_FILL_MODE)
  1197. command |= (1 << 31);
  1198. reg_write(recv->ohci, recv->ContextControlSet, command);
  1199. /* match on specified tags */
  1200. contextMatch = tag_mask << 28;
  1201. if (iso->channel == -1) {
  1202. /* enable multichannel reception */
  1203. reg_write(recv->ohci, recv->ContextControlSet, (1 << 28));
  1204. } else {
  1205. /* listen on channel */
  1206. contextMatch |= iso->channel;
  1207. }
  1208. if (cycle != -1) {
  1209. u32 seconds;
  1210. /* enable cycleMatch */
  1211. reg_write(recv->ohci, recv->ContextControlSet, (1 << 29));
  1212. /* set starting cycle */
  1213. cycle &= 0x1FFF;
  1214. /* 'cycle' is only mod 8000, but we also need two 'seconds' bits -
  1215. just snarf them from the current time */
  1216. seconds = reg_read(recv->ohci, OHCI1394_IsochronousCycleTimer) >> 25;
  1217. /* advance one second to give some extra time for DMA to start */
  1218. seconds += 1;
  1219. cycle |= (seconds & 3) << 13;
  1220. contextMatch |= cycle << 12;
  1221. }
  1222. if (sync != -1) {
  1223. /* set sync flag on first DMA descriptor */
  1224. struct dma_cmd *cmd = &recv->block[recv->block_dma];
  1225. cmd->control |= cpu_to_le32(DMA_CTL_WAIT);
  1226. /* match sync field */
  1227. contextMatch |= (sync&0xf)<<8;
  1228. }
  1229. reg_write(recv->ohci, recv->ContextMatch, contextMatch);
  1230. /* address of first descriptor block */
  1231. command = dma_prog_region_offset_to_bus(&recv->prog,
  1232. recv->block_dma * sizeof(struct dma_cmd));
  1233. command |= 1; /* Z=1 */
  1234. reg_write(recv->ohci, recv->CommandPtr, command);
  1235. /* enable interrupts */
  1236. reg_write(recv->ohci, OHCI1394_IsoRecvIntMaskSet, 1 << recv->task.context);
  1237. wmb();
  1238. /* run */
  1239. reg_write(recv->ohci, recv->ContextControlSet, 0x8000);
  1240. /* issue a dummy read of the cycle timer register to force
  1241. all PCI writes to be posted immediately */
  1242. mb();
  1243. reg_read(recv->ohci, OHCI1394_IsochronousCycleTimer);
  1244. /* check RUN */
  1245. if (!(reg_read(recv->ohci, recv->ContextControlSet) & 0x8000)) {
  1246. PRINT(KERN_ERR,
  1247. "Error starting IR DMA (ContextControl 0x%08x)\n",
  1248. reg_read(recv->ohci, recv->ContextControlSet));
  1249. return -1;
  1250. }
  1251. return 0;
  1252. }
  1253. static void ohci_iso_recv_release_block(struct ohci_iso_recv *recv, int block)
  1254. {
  1255. /* re-use the DMA descriptor for the block */
  1256. /* by linking the previous descriptor to it */
  1257. int next_i = block;
  1258. int prev_i = (next_i == 0) ? (recv->nblocks - 1) : (next_i - 1);
  1259. struct dma_cmd *next = &recv->block[next_i];
  1260. struct dma_cmd *prev = &recv->block[prev_i];
  1261. /* 'next' becomes the new end of the DMA chain,
  1262. so disable branch and enable interrupt */
  1263. next->branchAddress = 0;
  1264. next->control |= cpu_to_le32(3 << 20);
  1265. next->status = cpu_to_le32(recv->buf_stride);
  1266. /* link prev to next */
  1267. prev->branchAddress = cpu_to_le32(dma_prog_region_offset_to_bus(&recv->prog,
  1268. sizeof(struct dma_cmd) * next_i)
  1269. | 1); /* Z=1 */
  1270. /* disable interrupt on previous DMA descriptor, except at intervals */
  1271. if ((prev_i % recv->block_irq_interval) == 0) {
  1272. prev->control |= cpu_to_le32(3 << 20); /* enable interrupt */
  1273. } else {
  1274. prev->control &= cpu_to_le32(~(3<<20)); /* disable interrupt */
  1275. }
  1276. wmb();
  1277. /* wake up DMA in case it fell asleep */
  1278. reg_write(recv->ohci, recv->ContextControlSet, (1 << 12));
  1279. }
  1280. static void ohci_iso_recv_bufferfill_release(struct ohci_iso_recv *recv,
  1281. struct hpsb_iso_packet_info *info)
  1282. {
  1283. int len;
  1284. /* release the memory where the packet was */
  1285. len = info->len;
  1286. /* add the wasted space for padding to 4 bytes */
  1287. if (len % 4)
  1288. len += 4 - (len % 4);
  1289. /* add 8 bytes for the OHCI DMA data format overhead */
  1290. len += 8;
  1291. recv->released_bytes += len;
  1292. /* have we released enough memory for one block? */
  1293. while (recv->released_bytes > recv->buf_stride) {
  1294. ohci_iso_recv_release_block(recv, recv->block_reader);
  1295. recv->block_reader = (recv->block_reader + 1) % recv->nblocks;
  1296. recv->released_bytes -= recv->buf_stride;
  1297. }
  1298. }
  1299. static inline void ohci_iso_recv_release(struct hpsb_iso *iso, struct hpsb_iso_packet_info *info)
  1300. {
  1301. struct ohci_iso_recv *recv = iso->hostdata;
  1302. if (recv->dma_mode == BUFFER_FILL_MODE) {
  1303. ohci_iso_recv_bufferfill_release(recv, info);
  1304. } else {
  1305. ohci_iso_recv_release_block(recv, info - iso->infos);
  1306. }
  1307. }
  1308. /* parse all packets from blocks that have been fully received */
  1309. static void ohci_iso_recv_bufferfill_parse(struct hpsb_iso *iso, struct ohci_iso_recv *recv)
  1310. {
  1311. int wake = 0;
  1312. int runaway = 0;
  1313. struct ti_ohci *ohci = recv->ohci;
  1314. while (1) {
  1315. /* we expect the next parsable packet to begin at recv->dma_offset */
  1316. /* note: packet layout is as shown in section 10.6.1.1 of the OHCI spec */
  1317. unsigned int offset;
  1318. unsigned short len, cycle;
  1319. unsigned char channel, tag, sy;
  1320. unsigned char *p = iso->data_buf.kvirt;
  1321. unsigned int this_block = recv->dma_offset/recv->buf_stride;
  1322. /* don't loop indefinitely */
  1323. if (runaway++ > 100000) {
  1324. atomic_inc(&iso->overflows);
  1325. PRINT(KERN_ERR,
  1326. "IR DMA error - Runaway during buffer parsing!\n");
  1327. break;
  1328. }
  1329. /* stop parsing once we arrive at block_dma (i.e. don't get ahead of DMA) */
  1330. if (this_block == recv->block_dma)
  1331. break;
  1332. wake = 1;
  1333. /* parse data length, tag, channel, and sy */
  1334. /* note: we keep our own local copies of 'len' and 'offset'
  1335. so the user can't mess with them by poking in the mmap area */
  1336. len = p[recv->dma_offset+2] | (p[recv->dma_offset+3] << 8);
  1337. if (len > 4096) {
  1338. PRINT(KERN_ERR,
  1339. "IR DMA error - bogus 'len' value %u\n", len);
  1340. }
  1341. channel = p[recv->dma_offset+1] & 0x3F;
  1342. tag = p[recv->dma_offset+1] >> 6;
  1343. sy = p[recv->dma_offset+0] & 0xF;
  1344. /* advance to data payload */
  1345. recv->dma_offset += 4;
  1346. /* check for wrap-around */
  1347. if (recv->dma_offset >= recv->buf_stride*recv->nblocks) {
  1348. recv->dma_offset -= recv->buf_stride*recv->nblocks;
  1349. }
  1350. /* dma_offset now points to the first byte of the data payload */
  1351. offset = recv->dma_offset;
  1352. /* advance to xferStatus/timeStamp */
  1353. recv->dma_offset += len;
  1354. /* payload is padded to 4 bytes */
  1355. if (len % 4) {
  1356. recv->dma_offset += 4 - (len%4);
  1357. }
  1358. /* check for wrap-around */
  1359. if (recv->dma_offset >= recv->buf_stride*recv->nblocks) {
  1360. /* uh oh, the packet data wraps from the last
  1361. to the first DMA block - make the packet
  1362. contiguous by copying its "tail" into the
  1363. guard page */
  1364. int guard_off = recv->buf_stride*recv->nblocks;
  1365. int tail_len = len - (guard_off - offset);
  1366. if (tail_len > 0 && tail_len < recv->buf_stride) {
  1367. memcpy(iso->data_buf.kvirt + guard_off,
  1368. iso->data_buf.kvirt,
  1369. tail_len);
  1370. }
  1371. recv->dma_offset -= recv->buf_stride*recv->nblocks;
  1372. }
  1373. /* parse timestamp */
  1374. cycle = p[recv->dma_offset+0] | (p[recv->dma_offset+1]<<8);
  1375. cycle &= 0x1FFF;
  1376. /* advance to next packet */
  1377. recv->dma_offset += 4;
  1378. /* check for wrap-around */
  1379. if (recv->dma_offset >= recv->buf_stride*recv->nblocks) {
  1380. recv->dma_offset -= recv->buf_stride*recv->nblocks;
  1381. }
  1382. hpsb_iso_packet_received(iso, offset, len, cycle, channel, tag, sy);
  1383. }
  1384. if (wake)
  1385. hpsb_iso_wake(iso);
  1386. }
  1387. static void ohci_iso_recv_bufferfill_task(struct hpsb_iso *iso, struct ohci_iso_recv *recv)
  1388. {
  1389. int loop;
  1390. struct ti_ohci *ohci = recv->ohci;
  1391. /* loop over all blocks */
  1392. for (loop = 0; loop < recv->nblocks; loop++) {
  1393. /* check block_dma to see if it's done */
  1394. struct dma_cmd *im = &recv->block[recv->block_dma];
  1395. /* check the DMA descriptor for new writes to xferStatus */
  1396. u16 xferstatus = le32_to_cpu(im->status) >> 16;
  1397. /* rescount is the number of bytes *remaining to be written* in the block */
  1398. u16 rescount = le32_to_cpu(im->status) & 0xFFFF;
  1399. unsigned char event = xferstatus & 0x1F;
  1400. if (!event) {
  1401. /* nothing has happened to this block yet */
  1402. break;
  1403. }
  1404. if (event != 0x11) {
  1405. atomic_inc(&iso->overflows);
  1406. PRINT(KERN_ERR,
  1407. "IR DMA error - OHCI error code 0x%02x\n", event);
  1408. }
  1409. if (rescount != 0) {
  1410. /* the card is still writing to this block;
  1411. we can't touch it until it's done */
  1412. break;
  1413. }
  1414. /* OK, the block is finished... */
  1415. /* sync our view of the block */
  1416. dma_region_sync_for_cpu(&iso->data_buf, recv->block_dma*recv->buf_stride, recv->buf_stride);
  1417. /* reset the DMA descriptor */
  1418. im->status = recv->buf_stride;
  1419. /* advance block_dma */
  1420. recv->block_dma = (recv->block_dma + 1) % recv->nblocks;
  1421. if ((recv->block_dma+1) % recv->nblocks == recv->block_reader) {
  1422. atomic_inc(&iso->overflows);
  1423. DBGMSG("ISO reception overflow - "
  1424. "ran out of DMA blocks");
  1425. }
  1426. }
  1427. /* parse any packets that have arrived */
  1428. ohci_iso_recv_bufferfill_parse(iso, recv);
  1429. }
  1430. static void ohci_iso_recv_packetperbuf_task(struct hpsb_iso *iso, struct ohci_iso_recv *recv)
  1431. {
  1432. int count;
  1433. int wake = 0;
  1434. struct ti_ohci *ohci = recv->ohci;
  1435. /* loop over the entire buffer */
  1436. for (count = 0; count < recv->nblocks; count++) {
  1437. u32 packet_len = 0;
  1438. /* pointer to the DMA descriptor */
  1439. struct dma_cmd *il = ((struct dma_cmd*) recv->prog.kvirt) + iso->pkt_dma;
  1440. /* check the DMA descriptor for new writes to xferStatus */
  1441. u16 xferstatus = le32_to_cpu(il->status) >> 16;
  1442. u16 rescount = le32_to_cpu(il->status) & 0xFFFF;
  1443. unsigned char event = xferstatus & 0x1F;
  1444. if (!event) {
  1445. /* this packet hasn't come in yet; we are done for now */
  1446. goto out;
  1447. }
  1448. if (event == 0x11) {
  1449. /* packet received successfully! */
  1450. /* rescount is the number of bytes *remaining* in the packet buffer,
  1451. after the packet was written */
  1452. packet_len = recv->buf_stride - rescount;
  1453. } else if (event == 0x02) {
  1454. PRINT(KERN_ERR, "IR DMA error - packet too long for buffer\n");
  1455. } else if (event) {
  1456. PRINT(KERN_ERR, "IR DMA error - OHCI error code 0x%02x\n", event);
  1457. }
  1458. /* sync our view of the buffer */
  1459. dma_region_sync_for_cpu(&iso->data_buf, iso->pkt_dma * recv->buf_stride, recv->buf_stride);
  1460. /* record the per-packet info */
  1461. {
  1462. /* iso header is 8 bytes ahead of the data payload */
  1463. unsigned char *hdr;
  1464. unsigned int offset;
  1465. unsigned short cycle;
  1466. unsigned char channel, tag, sy;
  1467. offset = iso->pkt_dma * recv->buf_stride;
  1468. hdr = iso->data_buf.kvirt + offset;
  1469. /* skip iso header */
  1470. offset += 8;
  1471. packet_len -= 8;
  1472. cycle = (hdr[0] | (hdr[1] << 8)) & 0x1FFF;
  1473. channel = hdr[5] & 0x3F;
  1474. tag = hdr[5] >> 6;
  1475. sy = hdr[4] & 0xF;
  1476. hpsb_iso_packet_received(iso, offset, packet_len, cycle, channel, tag, sy);
  1477. }
  1478. /* reset the DMA descriptor */
  1479. il->status = recv->buf_stride;
  1480. wake = 1;
  1481. recv->block_dma = iso->pkt_dma;
  1482. }
  1483. out:
  1484. if (wake)
  1485. hpsb_iso_wake(iso);
  1486. }
  1487. static void ohci_iso_recv_task(unsigned long data)
  1488. {
  1489. struct hpsb_iso *iso = (struct hpsb_iso*) data;
  1490. struct ohci_iso_recv *recv = iso->hostdata;
  1491. if (recv->dma_mode == BUFFER_FILL_MODE)
  1492. ohci_iso_recv_bufferfill_task(iso, recv);
  1493. else
  1494. ohci_iso_recv_packetperbuf_task(iso, recv);
  1495. }
  1496. /***********************************
  1497. * rawiso ISO transmission *
  1498. ***********************************/
  1499. struct ohci_iso_xmit {
  1500. struct ti_ohci *ohci;
  1501. struct dma_prog_region prog;
  1502. struct ohci1394_iso_tasklet task;
  1503. int task_active;
  1504. u32 ContextControlSet;
  1505. u32 ContextControlClear;
  1506. u32 CommandPtr;
  1507. };
  1508. /* transmission DMA program:
  1509. one OUTPUT_MORE_IMMEDIATE for the IT header
  1510. one OUTPUT_LAST for the buffer data */
  1511. struct iso_xmit_cmd {
  1512. struct dma_cmd output_more_immediate;
  1513. u8 iso_hdr[8];
  1514. u32 unused[2];
  1515. struct dma_cmd output_last;
  1516. };
  1517. static int ohci_iso_xmit_init(struct hpsb_iso *iso);
  1518. static int ohci_iso_xmit_start(struct hpsb_iso *iso, int cycle);
  1519. static void ohci_iso_xmit_shutdown(struct hpsb_iso *iso);
  1520. static void ohci_iso_xmit_task(unsigned long data);
  1521. static int ohci_iso_xmit_init(struct hpsb_iso *iso)
  1522. {
  1523. struct ohci_iso_xmit *xmit;
  1524. unsigned int prog_size;
  1525. int ctx;
  1526. int ret = -ENOMEM;
  1527. xmit = kmalloc(sizeof(*xmit), SLAB_KERNEL);
  1528. if (!xmit)
  1529. return -ENOMEM;
  1530. iso->hostdata = xmit;
  1531. xmit->ohci = iso->host->hostdata;
  1532. xmit->task_active = 0;
  1533. dma_prog_region_init(&xmit->prog);
  1534. prog_size = sizeof(struct iso_xmit_cmd) * iso->buf_packets;
  1535. if (dma_prog_region_alloc(&xmit->prog, prog_size, xmit->ohci->dev))
  1536. goto err;
  1537. ohci1394_init_iso_tasklet(&xmit->task, OHCI_ISO_TRANSMIT,
  1538. ohci_iso_xmit_task, (unsigned long) iso);
  1539. if (ohci1394_register_iso_tasklet(xmit->ohci, &xmit->task) < 0)
  1540. goto err;
  1541. xmit->task_active = 1;
  1542. /* xmit context registers are spaced 16 bytes apart */
  1543. ctx = xmit->task.context;
  1544. xmit->ContextControlSet = OHCI1394_IsoXmitContextControlSet + 16 * ctx;
  1545. xmit->ContextControlClear = OHCI1394_IsoXmitContextControlClear + 16 * ctx;
  1546. xmit->CommandPtr = OHCI1394_IsoXmitCommandPtr + 16 * ctx;
  1547. return 0;
  1548. err:
  1549. ohci_iso_xmit_shutdown(iso);
  1550. return ret;
  1551. }
  1552. static void ohci_iso_xmit_stop(struct hpsb_iso *iso)
  1553. {
  1554. struct ohci_iso_xmit *xmit = iso->hostdata;
  1555. struct ti_ohci *ohci = xmit->ohci;
  1556. /* disable interrupts */
  1557. reg_write(xmit->ohci, OHCI1394_IsoXmitIntMaskClear, 1 << xmit->task.context);
  1558. /* halt DMA */
  1559. if (ohci1394_stop_context(xmit->ohci, xmit->ContextControlClear, NULL)) {
  1560. /* XXX the DMA context will lock up if you try to send too much data! */
  1561. PRINT(KERN_ERR,
  1562. "you probably exceeded the OHCI card's bandwidth limit - "
  1563. "reload the module and reduce xmit bandwidth");
  1564. }
  1565. }
  1566. static void ohci_iso_xmit_shutdown(struct hpsb_iso *iso)
  1567. {
  1568. struct ohci_iso_xmit *xmit = iso->hostdata;
  1569. if (xmit->task_active) {
  1570. ohci_iso_xmit_stop(iso);
  1571. ohci1394_unregister_iso_tasklet(xmit->ohci, &xmit->task);
  1572. xmit->task_active = 0;
  1573. }
  1574. dma_prog_region_free(&xmit->prog);
  1575. kfree(xmit);
  1576. iso->hostdata = NULL;
  1577. }
  1578. static void ohci_iso_xmit_task(unsigned long data)
  1579. {
  1580. struct hpsb_iso *iso = (struct hpsb_iso*) data;
  1581. struct ohci_iso_xmit *xmit = iso->hostdata;
  1582. struct ti_ohci *ohci = xmit->ohci;
  1583. int wake = 0;
  1584. int count;
  1585. /* check the whole buffer if necessary, starting at pkt_dma */
  1586. for (count = 0; count < iso->buf_packets; count++) {
  1587. int cycle;
  1588. /* DMA descriptor */
  1589. struct iso_xmit_cmd *cmd = dma_region_i(&xmit->prog, struct iso_xmit_cmd, iso->pkt_dma);
  1590. /* check for new writes to xferStatus */
  1591. u16 xferstatus = le32_to_cpu(cmd->output_last.status) >> 16;
  1592. u8 event = xferstatus & 0x1F;
  1593. if (!event) {
  1594. /* packet hasn't been sent yet; we are done for now */
  1595. break;
  1596. }
  1597. if (event != 0x11)
  1598. PRINT(KERN_ERR,
  1599. "IT DMA error - OHCI error code 0x%02x\n", event);
  1600. /* at least one packet went out, so wake up the writer */
  1601. wake = 1;
  1602. /* parse cycle */
  1603. cycle = le32_to_cpu(cmd->output_last.status) & 0x1FFF;
  1604. /* tell the subsystem the packet has gone out */
  1605. hpsb_iso_packet_sent(iso, cycle, event != 0x11);
  1606. /* reset the DMA descriptor for next time */
  1607. cmd->output_last.status = 0;
  1608. }
  1609. if (wake)
  1610. hpsb_iso_wake(iso);
  1611. }
  1612. static int ohci_iso_xmit_queue(struct hpsb_iso *iso, struct hpsb_iso_packet_info *info)
  1613. {
  1614. struct ohci_iso_xmit *xmit = iso->hostdata;
  1615. struct ti_ohci *ohci = xmit->ohci;
  1616. int next_i, prev_i;
  1617. struct iso_xmit_cmd *next, *prev;
  1618. unsigned int offset;
  1619. unsigned short len;
  1620. unsigned char tag, sy;
  1621. /* check that the packet doesn't cross a page boundary
  1622. (we could allow this if we added OUTPUT_MORE descriptor support) */
  1623. if (cross_bound(info->offset, info->len)) {
  1624. PRINT(KERN_ERR,
  1625. "rawiso xmit: packet %u crosses a page boundary",
  1626. iso->first_packet);
  1627. return -EINVAL;
  1628. }
  1629. offset = info->offset;
  1630. len = info->len;
  1631. tag = info->tag;
  1632. sy = info->sy;
  1633. /* sync up the card's view of the buffer */
  1634. dma_region_sync_for_device(&iso->data_buf, offset, len);
  1635. /* append first_packet to the DMA chain */
  1636. /* by linking the previous descriptor to it */
  1637. /* (next will become the new end of the DMA chain) */
  1638. next_i = iso->first_packet;
  1639. prev_i = (next_i == 0) ? (iso->buf_packets - 1) : (next_i - 1);
  1640. next = dma_region_i(&xmit->prog, struct iso_xmit_cmd, next_i);
  1641. prev = dma_region_i(&xmit->prog, struct iso_xmit_cmd, prev_i);
  1642. /* set up the OUTPUT_MORE_IMMEDIATE descriptor */
  1643. memset(next, 0, sizeof(struct iso_xmit_cmd));
  1644. next->output_more_immediate.control = cpu_to_le32(0x02000008);
  1645. /* ISO packet header is embedded in the OUTPUT_MORE_IMMEDIATE */
  1646. /* tcode = 0xA, and sy */
  1647. next->iso_hdr[0] = 0xA0 | (sy & 0xF);
  1648. /* tag and channel number */
  1649. next->iso_hdr[1] = (tag << 6) | (iso->channel & 0x3F);
  1650. /* transmission speed */
  1651. next->iso_hdr[2] = iso->speed & 0x7;
  1652. /* payload size */
  1653. next->iso_hdr[6] = len & 0xFF;
  1654. next->iso_hdr[7] = len >> 8;
  1655. /* set up the OUTPUT_LAST */
  1656. next->output_last.control = cpu_to_le32(1 << 28);
  1657. next->output_last.control |= cpu_to_le32(1 << 27); /* update timeStamp */
  1658. next->output_last.control |= cpu_to_le32(3 << 20); /* want interrupt */
  1659. next->output_last.control |= cpu_to_le32(3 << 18); /* enable branch */
  1660. next->output_last.control |= cpu_to_le32(len);
  1661. /* payload bus address */
  1662. next->output_last.address = cpu_to_le32(dma_region_offset_to_bus(&iso->data_buf, offset));
  1663. /* leave branchAddress at zero for now */
  1664. /* re-write the previous DMA descriptor to chain to this one */
  1665. /* set prev branch address to point to next (Z=3) */
  1666. prev->output_last.branchAddress = cpu_to_le32(
  1667. dma_prog_region_offset_to_bus(&xmit->prog, sizeof(struct iso_xmit_cmd) * next_i) | 3);
  1668. /* disable interrupt, unless required by the IRQ interval */
  1669. if (prev_i % iso->irq_interval) {
  1670. prev->output_last.control &= cpu_to_le32(~(3 << 20)); /* no interrupt */
  1671. } else {
  1672. prev->output_last.control |= cpu_to_le32(3 << 20); /* enable interrupt */
  1673. }
  1674. wmb();
  1675. /* wake DMA in case it is sleeping */
  1676. reg_write(xmit->ohci, xmit->ContextControlSet, 1 << 12);
  1677. /* issue a dummy read of the cycle timer to force all PCI
  1678. writes to be posted immediately */
  1679. mb();
  1680. reg_read(xmit->ohci, OHCI1394_IsochronousCycleTimer);
  1681. return 0;
  1682. }
  1683. static int ohci_iso_xmit_start(struct hpsb_iso *iso, int cycle)
  1684. {
  1685. struct ohci_iso_xmit *xmit = iso->hostdata;
  1686. struct ti_ohci *ohci = xmit->ohci;
  1687. /* clear out the control register */
  1688. reg_write(xmit->ohci, xmit->ContextControlClear, 0xFFFFFFFF);
  1689. wmb();
  1690. /* address and length of first descriptor block (Z=3) */
  1691. reg_write(xmit->ohci, xmit->CommandPtr,
  1692. dma_prog_region_offset_to_bus(&xmit->prog, iso->pkt_dma * sizeof(struct iso_xmit_cmd)) | 3);
  1693. /* cycle match */
  1694. if (cycle != -1) {
  1695. u32 start = cycle & 0x1FFF;
  1696. /* 'cycle' is only mod 8000, but we also need two 'seconds' bits -
  1697. just snarf them from the current time */
  1698. u32 seconds = reg_read(xmit->ohci, OHCI1394_IsochronousCycleTimer) >> 25;
  1699. /* advance one second to give some extra time for DMA to start */
  1700. seconds += 1;
  1701. start |= (seconds & 3) << 13;
  1702. reg_write(xmit->ohci, xmit->ContextControlSet, 0x80000000 | (start << 16));
  1703. }
  1704. /* enable interrupts */
  1705. reg_write(xmit->ohci, OHCI1394_IsoXmitIntMaskSet, 1 << xmit->task.context);
  1706. /* run */
  1707. reg_write(xmit->ohci, xmit->ContextControlSet, 0x8000);
  1708. mb();
  1709. /* wait 100 usec to give the card time to go active */
  1710. udelay(100);
  1711. /* check the RUN bit */
  1712. if (!(reg_read(xmit->ohci, xmit->ContextControlSet) & 0x8000)) {
  1713. PRINT(KERN_ERR, "Error starting IT DMA (ContextControl 0x%08x)\n",
  1714. reg_read(xmit->ohci, xmit->ContextControlSet));
  1715. return -1;
  1716. }
  1717. return 0;
  1718. }
  1719. static int ohci_isoctl(struct hpsb_iso *iso, enum isoctl_cmd cmd, unsigned long arg)
  1720. {
  1721. switch(cmd) {
  1722. case XMIT_INIT:
  1723. return ohci_iso_xmit_init(iso);
  1724. case XMIT_START:
  1725. return ohci_iso_xmit_start(iso, arg);
  1726. case XMIT_STOP:
  1727. ohci_iso_xmit_stop(iso);
  1728. return 0;
  1729. case XMIT_QUEUE:
  1730. return ohci_iso_xmit_queue(iso, (struct hpsb_iso_packet_info*) arg);
  1731. case XMIT_SHUTDOWN:
  1732. ohci_iso_xmit_shutdown(iso);
  1733. return 0;
  1734. case RECV_INIT:
  1735. return ohci_iso_recv_init(iso);
  1736. case RECV_START: {
  1737. int *args = (int*) arg;
  1738. return ohci_iso_recv_start(iso, args[0], args[1], args[2]);
  1739. }
  1740. case RECV_STOP:
  1741. ohci_iso_recv_stop(iso);
  1742. return 0;
  1743. case RECV_RELEASE:
  1744. ohci_iso_recv_release(iso, (struct hpsb_iso_packet_info*) arg);
  1745. return 0;
  1746. case RECV_FLUSH:
  1747. ohci_iso_recv_task((unsigned long) iso);
  1748. return 0;
  1749. case RECV_SHUTDOWN:
  1750. ohci_iso_recv_shutdown(iso);
  1751. return 0;
  1752. case RECV_LISTEN_CHANNEL:
  1753. ohci_iso_recv_change_channel(iso, arg, 1);
  1754. return 0;
  1755. case RECV_UNLISTEN_CHANNEL:
  1756. ohci_iso_recv_change_channel(iso, arg, 0);
  1757. return 0;
  1758. case RECV_SET_CHANNEL_MASK:
  1759. ohci_iso_recv_set_channel_mask(iso, *((u64*) arg));
  1760. return 0;
  1761. default:
  1762. PRINT_G(KERN_ERR, "ohci_isoctl cmd %d not implemented yet",
  1763. cmd);
  1764. break;
  1765. }
  1766. return -EINVAL;
  1767. }
  1768. /***************************************
  1769. * IEEE-1394 functionality section END *
  1770. ***************************************/
  1771. /********************************************************
  1772. * Global stuff (interrupt handler, init/shutdown code) *
  1773. ********************************************************/
  1774. static void dma_trm_reset(struct dma_trm_ctx *d)
  1775. {
  1776. unsigned long flags;
  1777. LIST_HEAD(packet_list);
  1778. struct ti_ohci *ohci = d->ohci;
  1779. struct hpsb_packet *packet, *ptmp;
  1780. ohci1394_stop_context(ohci, d->ctrlClear, NULL);
  1781. /* Lock the context, reset it and release it. Move the packets
  1782. * that were pending in the context to packet_list and free
  1783. * them after releasing the lock. */
  1784. spin_lock_irqsave(&d->lock, flags);
  1785. list_splice(&d->fifo_list, &packet_list);
  1786. list_splice(&d->pending_list, &packet_list);
  1787. INIT_LIST_HEAD(&d->fifo_list);
  1788. INIT_LIST_HEAD(&d->pending_list);
  1789. d->branchAddrPtr = NULL;
  1790. d->sent_ind = d->prg_ind;
  1791. d->free_prgs = d->num_desc;
  1792. spin_unlock_irqrestore(&d->lock, flags);
  1793. if (list_empty(&packet_list))
  1794. return;
  1795. PRINT(KERN_INFO, "AT dma reset ctx=%d, aborting transmission", d->ctx);
  1796. /* Now process subsystem callbacks for the packets from this
  1797. * context. */
  1798. list_for_each_entry_safe(packet, ptmp, &packet_list, driver_list) {
  1799. list_del_init(&packet->driver_list);
  1800. hpsb_packet_sent(ohci->host, packet, ACKX_ABORTED);
  1801. }
  1802. }
  1803. static void ohci_schedule_iso_tasklets(struct ti_ohci *ohci,
  1804. quadlet_t rx_event,
  1805. quadlet_t tx_event)
  1806. {
  1807. struct ohci1394_iso_tasklet *t;
  1808. unsigned long mask;
  1809. spin_lock(&ohci->iso_tasklet_list_lock);
  1810. list_for_each_entry(t, &ohci->iso_tasklet_list, link) {
  1811. mask = 1 << t->context;
  1812. if (t->type == OHCI_ISO_TRANSMIT && tx_event & mask)
  1813. tasklet_schedule(&t->tasklet);
  1814. else if (rx_event & mask)
  1815. tasklet_schedule(&t->tasklet);
  1816. }
  1817. spin_unlock(&ohci->iso_tasklet_list_lock);
  1818. }
  1819. static irqreturn_t ohci_irq_handler(int irq, void *dev_id,
  1820. struct pt_regs *regs_are_unused)
  1821. {
  1822. quadlet_t event, node_id;
  1823. struct ti_ohci *ohci = (struct ti_ohci *)dev_id;
  1824. struct hpsb_host *host = ohci->host;
  1825. int phyid = -1, isroot = 0;
  1826. unsigned long flags;
  1827. /* Read and clear the interrupt event register. Don't clear
  1828. * the busReset event, though. This is done when we get the
  1829. * selfIDComplete interrupt. */
  1830. spin_lock_irqsave(&ohci->event_lock, flags);
  1831. event = reg_read(ohci, OHCI1394_IntEventClear);
  1832. reg_write(ohci, OHCI1394_IntEventClear, event & ~OHCI1394_busReset);
  1833. spin_unlock_irqrestore(&ohci->event_lock, flags);
  1834. if (!event)
  1835. return IRQ_NONE;
  1836. /* If event is ~(u32)0 cardbus card was ejected. In this case
  1837. * we just return, and clean up in the ohci1394_pci_remove
  1838. * function. */
  1839. if (event == ~(u32) 0) {
  1840. DBGMSG("Device removed.");
  1841. return IRQ_NONE;
  1842. }
  1843. DBGMSG("IntEvent: %08x", event);
  1844. if (event & OHCI1394_unrecoverableError) {
  1845. int ctx;
  1846. PRINT(KERN_ERR, "Unrecoverable error!");
  1847. if (reg_read(ohci, OHCI1394_AsReqTrContextControlSet) & 0x800)
  1848. PRINT(KERN_ERR, "Async Req Tx Context died: "
  1849. "ctrl[%08x] cmdptr[%08x]",
  1850. reg_read(ohci, OHCI1394_AsReqTrContextControlSet),
  1851. reg_read(ohci, OHCI1394_AsReqTrCommandPtr));
  1852. if (reg_read(ohci, OHCI1394_AsRspTrContextControlSet) & 0x800)
  1853. PRINT(KERN_ERR, "Async Rsp Tx Context died: "
  1854. "ctrl[%08x] cmdptr[%08x]",
  1855. reg_read(ohci, OHCI1394_AsRspTrContextControlSet),
  1856. reg_read(ohci, OHCI1394_AsRspTrCommandPtr));
  1857. if (reg_read(ohci, OHCI1394_AsReqRcvContextControlSet) & 0x800)
  1858. PRINT(KERN_ERR, "Async Req Rcv Context died: "
  1859. "ctrl[%08x] cmdptr[%08x]",
  1860. reg_read(ohci, OHCI1394_AsReqRcvContextControlSet),
  1861. reg_read(ohci, OHCI1394_AsReqRcvCommandPtr));
  1862. if (reg_read(ohci, OHCI1394_AsRspRcvContextControlSet) & 0x800)
  1863. PRINT(KERN_ERR, "Async Rsp Rcv Context died: "
  1864. "ctrl[%08x] cmdptr[%08x]",
  1865. reg_read(ohci, OHCI1394_AsRspRcvContextControlSet),
  1866. reg_read(ohci, OHCI1394_AsRspRcvCommandPtr));
  1867. for (ctx = 0; ctx < ohci->nb_iso_xmit_ctx; ctx++) {
  1868. if (reg_read(ohci, OHCI1394_IsoXmitContextControlSet + (16 * ctx)) & 0x800)
  1869. PRINT(KERN_ERR, "Iso Xmit %d Context died: "
  1870. "ctrl[%08x] cmdptr[%08x]", ctx,
  1871. reg_read(ohci, OHCI1394_IsoXmitContextControlSet + (16 * ctx)),
  1872. reg_read(ohci, OHCI1394_IsoXmitCommandPtr + (16 * ctx)));
  1873. }
  1874. for (ctx = 0; ctx < ohci->nb_iso_rcv_ctx; ctx++) {
  1875. if (reg_read(ohci, OHCI1394_IsoRcvContextControlSet + (32 * ctx)) & 0x800)
  1876. PRINT(KERN_ERR, "Iso Recv %d Context died: "
  1877. "ctrl[%08x] cmdptr[%08x] match[%08x]", ctx,
  1878. reg_read(ohci, OHCI1394_IsoRcvContextControlSet + (32 * ctx)),
  1879. reg_read(ohci, OHCI1394_IsoRcvCommandPtr + (32 * ctx)),
  1880. reg_read(ohci, OHCI1394_IsoRcvContextMatch + (32 * ctx)));
  1881. }
  1882. event &= ~OHCI1394_unrecoverableError;
  1883. }
  1884. if (event & OHCI1394_cycleInconsistent) {
  1885. /* We subscribe to the cycleInconsistent event only to
  1886. * clear the corresponding event bit... otherwise,
  1887. * isochronous cycleMatch DMA won't work. */
  1888. DBGMSG("OHCI1394_cycleInconsistent");
  1889. event &= ~OHCI1394_cycleInconsistent;
  1890. }
  1891. if (event & OHCI1394_busReset) {
  1892. /* The busReset event bit can't be cleared during the
  1893. * selfID phase, so we disable busReset interrupts, to
  1894. * avoid burying the cpu in interrupt requests. */
  1895. spin_lock_irqsave(&ohci->event_lock, flags);
  1896. reg_write(ohci, OHCI1394_IntMaskClear, OHCI1394_busReset);
  1897. if (ohci->check_busreset) {
  1898. int loop_count = 0;
  1899. udelay(10);
  1900. while (reg_read(ohci, OHCI1394_IntEventSet) & OHCI1394_busReset) {
  1901. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  1902. spin_unlock_irqrestore(&ohci->event_lock, flags);
  1903. udelay(10);
  1904. spin_lock_irqsave(&ohci->event_lock, flags);
  1905. /* The loop counter check is to prevent the driver
  1906. * from remaining in this state forever. For the
  1907. * initial bus reset, the loop continues for ever
  1908. * and the system hangs, until some device is plugged-in
  1909. * or out manually into a port! The forced reset seems
  1910. * to solve this problem. This mainly effects nForce2. */
  1911. if (loop_count > 10000) {
  1912. ohci_devctl(host, RESET_BUS, LONG_RESET);
  1913. DBGMSG("Detected bus-reset loop. Forced a bus reset!");
  1914. loop_count = 0;
  1915. }
  1916. loop_count++;
  1917. }
  1918. }
  1919. spin_unlock_irqrestore(&ohci->event_lock, flags);
  1920. if (!host->in_bus_reset) {
  1921. DBGMSG("irq_handler: Bus reset requested");
  1922. /* Subsystem call */
  1923. hpsb_bus_reset(ohci->host);
  1924. }
  1925. event &= ~OHCI1394_busReset;
  1926. }
  1927. if (event & OHCI1394_reqTxComplete) {
  1928. struct dma_trm_ctx *d = &ohci->at_req_context;
  1929. DBGMSG("Got reqTxComplete interrupt "
  1930. "status=0x%08X", reg_read(ohci, d->ctrlSet));
  1931. if (reg_read(ohci, d->ctrlSet) & 0x800)
  1932. ohci1394_stop_context(ohci, d->ctrlClear,
  1933. "reqTxComplete");
  1934. else
  1935. dma_trm_tasklet((unsigned long)d);
  1936. //tasklet_schedule(&d->task);
  1937. event &= ~OHCI1394_reqTxComplete;
  1938. }
  1939. if (event & OHCI1394_respTxComplete) {
  1940. struct dma_trm_ctx *d = &ohci->at_resp_context;
  1941. DBGMSG("Got respTxComplete interrupt "
  1942. "status=0x%08X", reg_read(ohci, d->ctrlSet));
  1943. if (reg_read(ohci, d->ctrlSet) & 0x800)
  1944. ohci1394_stop_context(ohci, d->ctrlClear,
  1945. "respTxComplete");
  1946. else
  1947. tasklet_schedule(&d->task);
  1948. event &= ~OHCI1394_respTxComplete;
  1949. }
  1950. if (event & OHCI1394_RQPkt) {
  1951. struct dma_rcv_ctx *d = &ohci->ar_req_context;
  1952. DBGMSG("Got RQPkt interrupt status=0x%08X",
  1953. reg_read(ohci, d->ctrlSet));
  1954. if (reg_read(ohci, d->ctrlSet) & 0x800)
  1955. ohci1394_stop_context(ohci, d->ctrlClear, "RQPkt");
  1956. else
  1957. tasklet_schedule(&d->task);
  1958. event &= ~OHCI1394_RQPkt;
  1959. }
  1960. if (event & OHCI1394_RSPkt) {
  1961. struct dma_rcv_ctx *d = &ohci->ar_resp_context;
  1962. DBGMSG("Got RSPkt interrupt status=0x%08X",
  1963. reg_read(ohci, d->ctrlSet));
  1964. if (reg_read(ohci, d->ctrlSet) & 0x800)
  1965. ohci1394_stop_context(ohci, d->ctrlClear, "RSPkt");
  1966. else
  1967. tasklet_schedule(&d->task);
  1968. event &= ~OHCI1394_RSPkt;
  1969. }
  1970. if (event & OHCI1394_isochRx) {
  1971. quadlet_t rx_event;
  1972. rx_event = reg_read(ohci, OHCI1394_IsoRecvIntEventSet);
  1973. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, rx_event);
  1974. ohci_schedule_iso_tasklets(ohci, rx_event, 0);
  1975. event &= ~OHCI1394_isochRx;
  1976. }
  1977. if (event & OHCI1394_isochTx) {
  1978. quadlet_t tx_event;
  1979. tx_event = reg_read(ohci, OHCI1394_IsoXmitIntEventSet);
  1980. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, tx_event);
  1981. ohci_schedule_iso_tasklets(ohci, 0, tx_event);
  1982. event &= ~OHCI1394_isochTx;
  1983. }
  1984. if (event & OHCI1394_selfIDComplete) {
  1985. if (host->in_bus_reset) {
  1986. node_id = reg_read(ohci, OHCI1394_NodeID);
  1987. if (!(node_id & 0x80000000)) {
  1988. PRINT(KERN_ERR,
  1989. "SelfID received, but NodeID invalid "
  1990. "(probably new bus reset occurred): %08X",
  1991. node_id);
  1992. goto selfid_not_valid;
  1993. }
  1994. phyid = node_id & 0x0000003f;
  1995. isroot = (node_id & 0x40000000) != 0;
  1996. DBGMSG("SelfID interrupt received "
  1997. "(phyid %d, %s)", phyid,
  1998. (isroot ? "root" : "not root"));
  1999. handle_selfid(ohci, host, phyid, isroot);
  2000. /* Clear the bus reset event and re-enable the
  2001. * busReset interrupt. */
  2002. spin_lock_irqsave(&ohci->event_lock, flags);
  2003. reg_write(ohci, OHCI1394_IntEventClear, OHCI1394_busReset);
  2004. reg_write(ohci, OHCI1394_IntMaskSet, OHCI1394_busReset);
  2005. spin_unlock_irqrestore(&ohci->event_lock, flags);
  2006. /* Accept Physical requests from all nodes. */
  2007. reg_write(ohci,OHCI1394_AsReqFilterHiSet, 0xffffffff);
  2008. reg_write(ohci,OHCI1394_AsReqFilterLoSet, 0xffffffff);
  2009. /* Turn on phys dma reception.
  2010. *
  2011. * TODO: Enable some sort of filtering management.
  2012. */
  2013. if (phys_dma) {
  2014. reg_write(ohci,OHCI1394_PhyReqFilterHiSet, 0xffffffff);
  2015. reg_write(ohci,OHCI1394_PhyReqFilterLoSet, 0xffffffff);
  2016. reg_write(ohci,OHCI1394_PhyUpperBound, 0xffff0000);
  2017. } else {
  2018. reg_write(ohci,OHCI1394_PhyReqFilterHiSet, 0x00000000);
  2019. reg_write(ohci,OHCI1394_PhyReqFilterLoSet, 0x00000000);
  2020. }
  2021. DBGMSG("PhyReqFilter=%08x%08x",
  2022. reg_read(ohci,OHCI1394_PhyReqFilterHiSet),
  2023. reg_read(ohci,OHCI1394_PhyReqFilterLoSet));
  2024. hpsb_selfid_complete(host, phyid, isroot);
  2025. } else
  2026. PRINT(KERN_ERR,
  2027. "SelfID received outside of bus reset sequence");
  2028. selfid_not_valid:
  2029. event &= ~OHCI1394_selfIDComplete;
  2030. }
  2031. /* Make sure we handle everything, just in case we accidentally
  2032. * enabled an interrupt that we didn't write a handler for. */
  2033. if (event)
  2034. PRINT(KERN_ERR, "Unhandled interrupt(s) 0x%08x",
  2035. event);
  2036. return IRQ_HANDLED;
  2037. }
  2038. /* Put the buffer back into the dma context */
  2039. static void insert_dma_buffer(struct dma_rcv_ctx *d, int idx)
  2040. {
  2041. struct ti_ohci *ohci = (struct ti_ohci*)(d->ohci);
  2042. DBGMSG("Inserting dma buf ctx=%d idx=%d", d->ctx, idx);
  2043. d->prg_cpu[idx]->status = cpu_to_le32(d->buf_size);
  2044. d->prg_cpu[idx]->branchAddress &= le32_to_cpu(0xfffffff0);
  2045. idx = (idx + d->num_desc - 1 ) % d->num_desc;
  2046. d->prg_cpu[idx]->branchAddress |= le32_to_cpu(0x00000001);
  2047. /* To avoid a race, ensure 1394 interface hardware sees the inserted
  2048. * context program descriptors before it sees the wakeup bit set. */
  2049. wmb();
  2050. /* wake up the dma context if necessary */
  2051. if (!(reg_read(ohci, d->ctrlSet) & 0x400)) {
  2052. PRINT(KERN_INFO,
  2053. "Waking dma ctx=%d ... processing is probably too slow",
  2054. d->ctx);
  2055. }
  2056. /* do this always, to avoid race condition */
  2057. reg_write(ohci, d->ctrlSet, 0x1000);
  2058. }
  2059. #define cond_le32_to_cpu(data, noswap) \
  2060. (noswap ? data : le32_to_cpu(data))
  2061. static const int TCODE_SIZE[16] = {20, 0, 16, -1, 16, 20, 20, 0,
  2062. -1, 0, -1, 0, -1, -1, 16, -1};
  2063. /*
  2064. * Determine the length of a packet in the buffer
  2065. * Optimization suggested by Pascal Drolet <pascal.drolet@informission.ca>
  2066. */
  2067. static __inline__ int packet_length(struct dma_rcv_ctx *d, int idx, quadlet_t *buf_ptr,
  2068. int offset, unsigned char tcode, int noswap)
  2069. {
  2070. int length = -1;
  2071. if (d->type == DMA_CTX_ASYNC_REQ || d->type == DMA_CTX_ASYNC_RESP) {
  2072. length = TCODE_SIZE[tcode];
  2073. if (length == 0) {
  2074. if (offset + 12 >= d->buf_size) {
  2075. length = (cond_le32_to_cpu(d->buf_cpu[(idx + 1) % d->num_desc]
  2076. [3 - ((d->buf_size - offset) >> 2)], noswap) >> 16);
  2077. } else {
  2078. length = (cond_le32_to_cpu(buf_ptr[3], noswap) >> 16);
  2079. }
  2080. length += 20;
  2081. }
  2082. } else if (d->type == DMA_CTX_ISO) {
  2083. /* Assumption: buffer fill mode with header/trailer */
  2084. length = (cond_le32_to_cpu(buf_ptr[0], noswap) >> 16) + 8;
  2085. }
  2086. if (length > 0 && length % 4)
  2087. length += 4 - (length % 4);
  2088. return length;
  2089. }
  2090. /* Tasklet that processes dma receive buffers */
  2091. static void dma_rcv_tasklet (unsigned long data)
  2092. {
  2093. struct dma_rcv_ctx *d = (struct dma_rcv_ctx*)data;
  2094. struct ti_ohci *ohci = (struct ti_ohci*)(d->ohci);
  2095. unsigned int split_left, idx, offset, rescount;
  2096. unsigned char tcode;
  2097. int length, bytes_left, ack;
  2098. unsigned long flags;
  2099. quadlet_t *buf_ptr;
  2100. char *split_ptr;
  2101. char msg[256];
  2102. spin_lock_irqsave(&d->lock, flags);
  2103. idx = d->buf_ind;
  2104. offset = d->buf_offset;
  2105. buf_ptr = d->buf_cpu[idx] + offset/4;
  2106. rescount = le32_to_cpu(d->prg_cpu[idx]->status) & 0xffff;
  2107. bytes_left = d->buf_size - rescount - offset;
  2108. while (bytes_left > 0) {
  2109. tcode = (cond_le32_to_cpu(buf_ptr[0], ohci->no_swap_incoming) >> 4) & 0xf;
  2110. /* packet_length() will return < 4 for an error */
  2111. length = packet_length(d, idx, buf_ptr, offset, tcode, ohci->no_swap_incoming);
  2112. if (length < 4) { /* something is wrong */
  2113. sprintf(msg,"Unexpected tcode 0x%x(0x%08x) in AR ctx=%d, length=%d",
  2114. tcode, cond_le32_to_cpu(buf_ptr[0], ohci->no_swap_incoming),
  2115. d->ctx, length);
  2116. ohci1394_stop_context(ohci, d->ctrlClear, msg);
  2117. spin_unlock_irqrestore(&d->lock, flags);
  2118. return;
  2119. }
  2120. /* The first case is where we have a packet that crosses
  2121. * over more than one descriptor. The next case is where
  2122. * it's all in the first descriptor. */
  2123. if ((offset + length) > d->buf_size) {
  2124. DBGMSG("Split packet rcv'd");
  2125. if (length > d->split_buf_size) {
  2126. ohci1394_stop_context(ohci, d->ctrlClear,
  2127. "Split packet size exceeded");
  2128. d->buf_ind = idx;
  2129. d->buf_offset = offset;
  2130. spin_unlock_irqrestore(&d->lock, flags);
  2131. return;
  2132. }
  2133. if (le32_to_cpu(d->prg_cpu[(idx+1)%d->num_desc]->status)
  2134. == d->buf_size) {
  2135. /* Other part of packet not written yet.
  2136. * this should never happen I think
  2137. * anyway we'll get it on the next call. */
  2138. PRINT(KERN_INFO,
  2139. "Got only half a packet!");
  2140. d->buf_ind = idx;
  2141. d->buf_offset = offset;
  2142. spin_unlock_irqrestore(&d->lock, flags);
  2143. return;
  2144. }
  2145. split_left = length;
  2146. split_ptr = (char *)d->spb;
  2147. memcpy(split_ptr,buf_ptr,d->buf_size-offset);
  2148. split_left -= d->buf_size-offset;
  2149. split_ptr += d->buf_size-offset;
  2150. insert_dma_buffer(d, idx);
  2151. idx = (idx+1) % d->num_desc;
  2152. buf_ptr = d->buf_cpu[idx];
  2153. offset=0;
  2154. while (split_left >= d->buf_size) {
  2155. memcpy(split_ptr,buf_ptr,d->buf_size);
  2156. split_ptr += d->buf_size;
  2157. split_left -= d->buf_size;
  2158. insert_dma_buffer(d, idx);
  2159. idx = (idx+1) % d->num_desc;
  2160. buf_ptr = d->buf_cpu[idx];
  2161. }
  2162. if (split_left > 0) {
  2163. memcpy(split_ptr, buf_ptr, split_left);
  2164. offset = split_left;
  2165. buf_ptr += offset/4;
  2166. }
  2167. } else {
  2168. DBGMSG("Single packet rcv'd");
  2169. memcpy(d->spb, buf_ptr, length);
  2170. offset += length;
  2171. buf_ptr += length/4;
  2172. if (offset==d->buf_size) {
  2173. insert_dma_buffer(d, idx);
  2174. idx = (idx+1) % d->num_desc;
  2175. buf_ptr = d->buf_cpu[idx];
  2176. offset=0;
  2177. }
  2178. }
  2179. /* We get one phy packet to the async descriptor for each
  2180. * bus reset. We always ignore it. */
  2181. if (tcode != OHCI1394_TCODE_PHY) {
  2182. if (!ohci->no_swap_incoming)
  2183. packet_swab(d->spb, tcode);
  2184. DBGMSG("Packet received from node"
  2185. " %d ack=0x%02X spd=%d tcode=0x%X"
  2186. " length=%d ctx=%d tlabel=%d",
  2187. (d->spb[1]>>16)&0x3f,
  2188. (cond_le32_to_cpu(d->spb[length/4-1], ohci->no_swap_incoming)>>16)&0x1f,
  2189. (cond_le32_to_cpu(d->spb[length/4-1], ohci->no_swap_incoming)>>21)&0x3,
  2190. tcode, length, d->ctx,
  2191. (cond_le32_to_cpu(d->spb[length/4-1], ohci->no_swap_incoming)>>10)&0x3f);
  2192. ack = (((cond_le32_to_cpu(d->spb[length/4-1], ohci->no_swap_incoming)>>16)&0x1f)
  2193. == 0x11) ? 1 : 0;
  2194. hpsb_packet_received(ohci->host, d->spb,
  2195. length-4, ack);
  2196. }
  2197. #ifdef OHCI1394_DEBUG
  2198. else
  2199. PRINT (KERN_DEBUG, "Got phy packet ctx=%d ... discarded",
  2200. d->ctx);
  2201. #endif
  2202. rescount = le32_to_cpu(d->prg_cpu[idx]->status) & 0xffff;
  2203. bytes_left = d->buf_size - rescount - offset;
  2204. }
  2205. d->buf_ind = idx;
  2206. d->buf_offset = offset;
  2207. spin_unlock_irqrestore(&d->lock, flags);
  2208. }
  2209. /* Bottom half that processes sent packets */
  2210. static void dma_trm_tasklet (unsigned long data)
  2211. {
  2212. struct dma_trm_ctx *d = (struct dma_trm_ctx*)data;
  2213. struct ti_ohci *ohci = (struct ti_ohci*)(d->ohci);
  2214. struct hpsb_packet *packet, *ptmp;
  2215. unsigned long flags;
  2216. u32 status, ack;
  2217. size_t datasize;
  2218. spin_lock_irqsave(&d->lock, flags);
  2219. list_for_each_entry_safe(packet, ptmp, &d->fifo_list, driver_list) {
  2220. datasize = packet->data_size;
  2221. if (datasize && packet->type != hpsb_raw)
  2222. status = le32_to_cpu(
  2223. d->prg_cpu[d->sent_ind]->end.status) >> 16;
  2224. else
  2225. status = le32_to_cpu(
  2226. d->prg_cpu[d->sent_ind]->begin.status) >> 16;
  2227. if (status == 0)
  2228. /* this packet hasn't been sent yet*/
  2229. break;
  2230. #ifdef OHCI1394_DEBUG
  2231. if (datasize)
  2232. if (((le32_to_cpu(d->prg_cpu[d->sent_ind]->data[0])>>4)&0xf) == 0xa)
  2233. DBGMSG("Stream packet sent to channel %d tcode=0x%X "
  2234. "ack=0x%X spd=%d dataLength=%d ctx=%d",
  2235. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[0])>>8)&0x3f,
  2236. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[0])>>4)&0xf,
  2237. status&0x1f, (status>>5)&0x3,
  2238. le32_to_cpu(d->prg_cpu[d->sent_ind]->data[1])>>16,
  2239. d->ctx);
  2240. else
  2241. DBGMSG("Packet sent to node %d tcode=0x%X tLabel="
  2242. "0x%02X ack=0x%X spd=%d dataLength=%d ctx=%d",
  2243. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[1])>>16)&0x3f,
  2244. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[0])>>4)&0xf,
  2245. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[0])>>10)&0x3f,
  2246. status&0x1f, (status>>5)&0x3,
  2247. le32_to_cpu(d->prg_cpu[d->sent_ind]->data[3])>>16,
  2248. d->ctx);
  2249. else
  2250. DBGMSG("Packet sent to node %d tcode=0x%X tLabel="
  2251. "0x%02X ack=0x%X spd=%d data=0x%08X ctx=%d",
  2252. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[1])
  2253. >>16)&0x3f,
  2254. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[0])
  2255. >>4)&0xf,
  2256. (le32_to_cpu(d->prg_cpu[d->sent_ind]->data[0])
  2257. >>10)&0x3f,
  2258. status&0x1f, (status>>5)&0x3,
  2259. le32_to_cpu(d->prg_cpu[d->sent_ind]->data[3]),
  2260. d->ctx);
  2261. #endif
  2262. if (status & 0x10) {
  2263. ack = status & 0xf;
  2264. } else {
  2265. switch (status & 0x1f) {
  2266. case EVT_NO_STATUS: /* that should never happen */
  2267. case EVT_RESERVED_A: /* that should never happen */
  2268. case EVT_LONG_PACKET: /* that should never happen */
  2269. PRINT(KERN_WARNING, "Received OHCI evt_* error 0x%x", status & 0x1f);
  2270. ack = ACKX_SEND_ERROR;
  2271. break;
  2272. case EVT_MISSING_ACK:
  2273. ack = ACKX_TIMEOUT;
  2274. break;
  2275. case EVT_UNDERRUN:
  2276. ack = ACKX_SEND_ERROR;
  2277. break;
  2278. case EVT_OVERRUN: /* that should never happen */
  2279. PRINT(KERN_WARNING, "Received OHCI evt_* error 0x%x", status & 0x1f);
  2280. ack = ACKX_SEND_ERROR;
  2281. break;
  2282. case EVT_DESCRIPTOR_READ:
  2283. case EVT_DATA_READ:
  2284. case EVT_DATA_WRITE:
  2285. ack = ACKX_SEND_ERROR;
  2286. break;
  2287. case EVT_BUS_RESET: /* that should never happen */
  2288. PRINT(KERN_WARNING, "Received OHCI evt_* error 0x%x", status & 0x1f);
  2289. ack = ACKX_SEND_ERROR;
  2290. break;
  2291. case EVT_TIMEOUT:
  2292. ack = ACKX_TIMEOUT;
  2293. break;
  2294. case EVT_TCODE_ERR:
  2295. ack = ACKX_SEND_ERROR;
  2296. break;
  2297. case EVT_RESERVED_B: /* that should never happen */
  2298. case EVT_RESERVED_C: /* that should never happen */
  2299. PRINT(KERN_WARNING, "Received OHCI evt_* error 0x%x", status & 0x1f);
  2300. ack = ACKX_SEND_ERROR;
  2301. break;
  2302. case EVT_UNKNOWN:
  2303. case EVT_FLUSHED:
  2304. ack = ACKX_SEND_ERROR;
  2305. break;
  2306. default:
  2307. PRINT(KERN_ERR, "Unhandled OHCI evt_* error 0x%x", status & 0x1f);
  2308. ack = ACKX_SEND_ERROR;
  2309. BUG();
  2310. }
  2311. }
  2312. list_del_init(&packet->driver_list);
  2313. hpsb_packet_sent(ohci->host, packet, ack);
  2314. if (datasize) {
  2315. pci_unmap_single(ohci->dev,
  2316. cpu_to_le32(d->prg_cpu[d->sent_ind]->end.address),
  2317. datasize, PCI_DMA_TODEVICE);
  2318. OHCI_DMA_FREE("single Xmit data packet");
  2319. }
  2320. d->sent_ind = (d->sent_ind+1)%d->num_desc;
  2321. d->free_prgs++;
  2322. }
  2323. dma_trm_flush(ohci, d);
  2324. spin_unlock_irqrestore(&d->lock, flags);
  2325. }
  2326. static void stop_dma_rcv_ctx(struct dma_rcv_ctx *d)
  2327. {
  2328. if (d->ctrlClear) {
  2329. ohci1394_stop_context(d->ohci, d->ctrlClear, NULL);
  2330. if (d->type == DMA_CTX_ISO) {
  2331. /* disable interrupts */
  2332. reg_write(d->ohci, OHCI1394_IsoRecvIntMaskClear, 1 << d->ctx);
  2333. ohci1394_unregister_iso_tasklet(d->ohci, &d->ohci->ir_legacy_tasklet);
  2334. } else {
  2335. tasklet_kill(&d->task);
  2336. }
  2337. }
  2338. }
  2339. static void free_dma_rcv_ctx(struct dma_rcv_ctx *d)
  2340. {
  2341. int i;
  2342. struct ti_ohci *ohci = d->ohci;
  2343. if (ohci == NULL)
  2344. return;
  2345. DBGMSG("Freeing dma_rcv_ctx %d", d->ctx);
  2346. if (d->buf_cpu) {
  2347. for (i=0; i<d->num_desc; i++)
  2348. if (d->buf_cpu[i] && d->buf_bus[i]) {
  2349. pci_free_consistent(
  2350. ohci->dev, d->buf_size,
  2351. d->buf_cpu[i], d->buf_bus[i]);
  2352. OHCI_DMA_FREE("consistent dma_rcv buf[%d]", i);
  2353. }
  2354. kfree(d->buf_cpu);
  2355. kfree(d->buf_bus);
  2356. }
  2357. if (d->prg_cpu) {
  2358. for (i=0; i<d->num_desc; i++)
  2359. if (d->prg_cpu[i] && d->prg_bus[i]) {
  2360. pci_pool_free(d->prg_pool, d->prg_cpu[i], d->prg_bus[i]);
  2361. OHCI_DMA_FREE("consistent dma_rcv prg[%d]", i);
  2362. }
  2363. pci_pool_destroy(d->prg_pool);
  2364. OHCI_DMA_FREE("dma_rcv prg pool");
  2365. kfree(d->prg_cpu);
  2366. kfree(d->prg_bus);
  2367. }
  2368. if (d->spb) kfree(d->spb);
  2369. /* Mark this context as freed. */
  2370. d->ohci = NULL;
  2371. }
  2372. static int
  2373. alloc_dma_rcv_ctx(struct ti_ohci *ohci, struct dma_rcv_ctx *d,
  2374. enum context_type type, int ctx, int num_desc,
  2375. int buf_size, int split_buf_size, int context_base)
  2376. {
  2377. int i, len;
  2378. static int num_allocs;
  2379. static char pool_name[20];
  2380. d->ohci = ohci;
  2381. d->type = type;
  2382. d->ctx = ctx;
  2383. d->num_desc = num_desc;
  2384. d->buf_size = buf_size;
  2385. d->split_buf_size = split_buf_size;
  2386. d->ctrlSet = 0;
  2387. d->ctrlClear = 0;
  2388. d->cmdPtr = 0;
  2389. d->buf_cpu = kmalloc(d->num_desc * sizeof(quadlet_t*), GFP_ATOMIC);
  2390. d->buf_bus = kmalloc(d->num_desc * sizeof(dma_addr_t), GFP_ATOMIC);
  2391. if (d->buf_cpu == NULL || d->buf_bus == NULL) {
  2392. PRINT(KERN_ERR, "Failed to allocate dma buffer");
  2393. free_dma_rcv_ctx(d);
  2394. return -ENOMEM;
  2395. }
  2396. memset(d->buf_cpu, 0, d->num_desc * sizeof(quadlet_t*));
  2397. memset(d->buf_bus, 0, d->num_desc * sizeof(dma_addr_t));
  2398. d->prg_cpu = kmalloc(d->num_desc * sizeof(struct dma_cmd*),
  2399. GFP_ATOMIC);
  2400. d->prg_bus = kmalloc(d->num_desc * sizeof(dma_addr_t), GFP_ATOMIC);
  2401. if (d->prg_cpu == NULL || d->prg_bus == NULL) {
  2402. PRINT(KERN_ERR, "Failed to allocate dma prg");
  2403. free_dma_rcv_ctx(d);
  2404. return -ENOMEM;
  2405. }
  2406. memset(d->prg_cpu, 0, d->num_desc * sizeof(struct dma_cmd*));
  2407. memset(d->prg_bus, 0, d->num_desc * sizeof(dma_addr_t));
  2408. d->spb = kmalloc(d->split_buf_size, GFP_ATOMIC);
  2409. if (d->spb == NULL) {
  2410. PRINT(KERN_ERR, "Failed to allocate split buffer");
  2411. free_dma_rcv_ctx(d);
  2412. return -ENOMEM;
  2413. }
  2414. len = sprintf(pool_name, "ohci1394_rcv_prg");
  2415. sprintf(pool_name+len, "%d", num_allocs);
  2416. d->prg_pool = pci_pool_create(pool_name, ohci->dev,
  2417. sizeof(struct dma_cmd), 4, 0);
  2418. if(d->prg_pool == NULL)
  2419. {
  2420. PRINT(KERN_ERR, "pci_pool_create failed for %s", pool_name);
  2421. free_dma_rcv_ctx(d);
  2422. return -ENOMEM;
  2423. }
  2424. num_allocs++;
  2425. OHCI_DMA_ALLOC("dma_rcv prg pool");
  2426. for (i=0; i<d->num_desc; i++) {
  2427. d->buf_cpu[i] = pci_alloc_consistent(ohci->dev,
  2428. d->buf_size,
  2429. d->buf_bus+i);
  2430. OHCI_DMA_ALLOC("consistent dma_rcv buf[%d]", i);
  2431. if (d->buf_cpu[i] != NULL) {
  2432. memset(d->buf_cpu[i], 0, d->buf_size);
  2433. } else {
  2434. PRINT(KERN_ERR,
  2435. "Failed to allocate dma buffer");
  2436. free_dma_rcv_ctx(d);
  2437. return -ENOMEM;
  2438. }
  2439. d->prg_cpu[i] = pci_pool_alloc(d->prg_pool, SLAB_KERNEL, d->prg_bus+i);
  2440. OHCI_DMA_ALLOC("pool dma_rcv prg[%d]", i);
  2441. if (d->prg_cpu[i] != NULL) {
  2442. memset(d->prg_cpu[i], 0, sizeof(struct dma_cmd));
  2443. } else {
  2444. PRINT(KERN_ERR,
  2445. "Failed to allocate dma prg");
  2446. free_dma_rcv_ctx(d);
  2447. return -ENOMEM;
  2448. }
  2449. }
  2450. spin_lock_init(&d->lock);
  2451. if (type == DMA_CTX_ISO) {
  2452. ohci1394_init_iso_tasklet(&ohci->ir_legacy_tasklet,
  2453. OHCI_ISO_MULTICHANNEL_RECEIVE,
  2454. dma_rcv_tasklet, (unsigned long) d);
  2455. if (ohci1394_register_iso_tasklet(ohci,
  2456. &ohci->ir_legacy_tasklet) < 0) {
  2457. PRINT(KERN_ERR, "No IR DMA context available");
  2458. free_dma_rcv_ctx(d);
  2459. return -EBUSY;
  2460. }
  2461. /* the IR context can be assigned to any DMA context
  2462. * by ohci1394_register_iso_tasklet */
  2463. d->ctx = ohci->ir_legacy_tasklet.context;
  2464. d->ctrlSet = OHCI1394_IsoRcvContextControlSet + 32*d->ctx;
  2465. d->ctrlClear = OHCI1394_IsoRcvContextControlClear + 32*d->ctx;
  2466. d->cmdPtr = OHCI1394_IsoRcvCommandPtr + 32*d->ctx;
  2467. d->ctxtMatch = OHCI1394_IsoRcvContextMatch + 32*d->ctx;
  2468. } else {
  2469. d->ctrlSet = context_base + OHCI1394_ContextControlSet;
  2470. d->ctrlClear = context_base + OHCI1394_ContextControlClear;
  2471. d->cmdPtr = context_base + OHCI1394_ContextCommandPtr;
  2472. tasklet_init (&d->task, dma_rcv_tasklet, (unsigned long) d);
  2473. }
  2474. return 0;
  2475. }
  2476. static void free_dma_trm_ctx(struct dma_trm_ctx *d)
  2477. {
  2478. int i;
  2479. struct ti_ohci *ohci = d->ohci;
  2480. if (ohci == NULL)
  2481. return;
  2482. DBGMSG("Freeing dma_trm_ctx %d", d->ctx);
  2483. if (d->prg_cpu) {
  2484. for (i=0; i<d->num_desc; i++)
  2485. if (d->prg_cpu[i] && d->prg_bus[i]) {
  2486. pci_pool_free(d->prg_pool, d->prg_cpu[i], d->prg_bus[i]);
  2487. OHCI_DMA_FREE("pool dma_trm prg[%d]", i);
  2488. }
  2489. pci_pool_destroy(d->prg_pool);
  2490. OHCI_DMA_FREE("dma_trm prg pool");
  2491. kfree(d->prg_cpu);
  2492. kfree(d->prg_bus);
  2493. }
  2494. /* Mark this context as freed. */
  2495. d->ohci = NULL;
  2496. }
  2497. static int
  2498. alloc_dma_trm_ctx(struct ti_ohci *ohci, struct dma_trm_ctx *d,
  2499. enum context_type type, int ctx, int num_desc,
  2500. int context_base)
  2501. {
  2502. int i, len;
  2503. static char pool_name[20];
  2504. static int num_allocs=0;
  2505. d->ohci = ohci;
  2506. d->type = type;
  2507. d->ctx = ctx;
  2508. d->num_desc = num_desc;
  2509. d->ctrlSet = 0;
  2510. d->ctrlClear = 0;
  2511. d->cmdPtr = 0;
  2512. d->prg_cpu = kmalloc(d->num_desc * sizeof(struct at_dma_prg*),
  2513. GFP_KERNEL);
  2514. d->prg_bus = kmalloc(d->num_desc * sizeof(dma_addr_t), GFP_KERNEL);
  2515. if (d->prg_cpu == NULL || d->prg_bus == NULL) {
  2516. PRINT(KERN_ERR, "Failed to allocate at dma prg");
  2517. free_dma_trm_ctx(d);
  2518. return -ENOMEM;
  2519. }
  2520. memset(d->prg_cpu, 0, d->num_desc * sizeof(struct at_dma_prg*));
  2521. memset(d->prg_bus, 0, d->num_desc * sizeof(dma_addr_t));
  2522. len = sprintf(pool_name, "ohci1394_trm_prg");
  2523. sprintf(pool_name+len, "%d", num_allocs);
  2524. d->prg_pool = pci_pool_create(pool_name, ohci->dev,
  2525. sizeof(struct at_dma_prg), 4, 0);
  2526. if (d->prg_pool == NULL) {
  2527. PRINT(KERN_ERR, "pci_pool_create failed for %s", pool_name);
  2528. free_dma_trm_ctx(d);
  2529. return -ENOMEM;
  2530. }
  2531. num_allocs++;
  2532. OHCI_DMA_ALLOC("dma_rcv prg pool");
  2533. for (i = 0; i < d->num_desc; i++) {
  2534. d->prg_cpu[i] = pci_pool_alloc(d->prg_pool, SLAB_KERNEL, d->prg_bus+i);
  2535. OHCI_DMA_ALLOC("pool dma_trm prg[%d]", i);
  2536. if (d->prg_cpu[i] != NULL) {
  2537. memset(d->prg_cpu[i], 0, sizeof(struct at_dma_prg));
  2538. } else {
  2539. PRINT(KERN_ERR,
  2540. "Failed to allocate at dma prg");
  2541. free_dma_trm_ctx(d);
  2542. return -ENOMEM;
  2543. }
  2544. }
  2545. spin_lock_init(&d->lock);
  2546. /* initialize tasklet */
  2547. if (type == DMA_CTX_ISO) {
  2548. ohci1394_init_iso_tasklet(&ohci->it_legacy_tasklet, OHCI_ISO_TRANSMIT,
  2549. dma_trm_tasklet, (unsigned long) d);
  2550. if (ohci1394_register_iso_tasklet(ohci,
  2551. &ohci->it_legacy_tasklet) < 0) {
  2552. PRINT(KERN_ERR, "No IT DMA context available");
  2553. free_dma_trm_ctx(d);
  2554. return -EBUSY;
  2555. }
  2556. /* IT can be assigned to any context by register_iso_tasklet */
  2557. d->ctx = ohci->it_legacy_tasklet.context;
  2558. d->ctrlSet = OHCI1394_IsoXmitContextControlSet + 16 * d->ctx;
  2559. d->ctrlClear = OHCI1394_IsoXmitContextControlClear + 16 * d->ctx;
  2560. d->cmdPtr = OHCI1394_IsoXmitCommandPtr + 16 * d->ctx;
  2561. } else {
  2562. d->ctrlSet = context_base + OHCI1394_ContextControlSet;
  2563. d->ctrlClear = context_base + OHCI1394_ContextControlClear;
  2564. d->cmdPtr = context_base + OHCI1394_ContextCommandPtr;
  2565. tasklet_init (&d->task, dma_trm_tasklet, (unsigned long)d);
  2566. }
  2567. return 0;
  2568. }
  2569. static void ohci_set_hw_config_rom(struct hpsb_host *host, quadlet_t *config_rom)
  2570. {
  2571. struct ti_ohci *ohci = host->hostdata;
  2572. reg_write(ohci, OHCI1394_ConfigROMhdr, be32_to_cpu(config_rom[0]));
  2573. reg_write(ohci, OHCI1394_BusOptions, be32_to_cpu(config_rom[2]));
  2574. memcpy(ohci->csr_config_rom_cpu, config_rom, OHCI_CONFIG_ROM_LEN);
  2575. }
  2576. static quadlet_t ohci_hw_csr_reg(struct hpsb_host *host, int reg,
  2577. quadlet_t data, quadlet_t compare)
  2578. {
  2579. struct ti_ohci *ohci = host->hostdata;
  2580. int i;
  2581. reg_write(ohci, OHCI1394_CSRData, data);
  2582. reg_write(ohci, OHCI1394_CSRCompareData, compare);
  2583. reg_write(ohci, OHCI1394_CSRControl, reg & 0x3);
  2584. for (i = 0; i < OHCI_LOOP_COUNT; i++) {
  2585. if (reg_read(ohci, OHCI1394_CSRControl) & 0x80000000)
  2586. break;
  2587. mdelay(1);
  2588. }
  2589. return reg_read(ohci, OHCI1394_CSRData);
  2590. }
  2591. static struct hpsb_host_driver ohci1394_driver = {
  2592. .owner = THIS_MODULE,
  2593. .name = OHCI1394_DRIVER_NAME,
  2594. .set_hw_config_rom = ohci_set_hw_config_rom,
  2595. .transmit_packet = ohci_transmit,
  2596. .devctl = ohci_devctl,
  2597. .isoctl = ohci_isoctl,
  2598. .hw_csr_reg = ohci_hw_csr_reg,
  2599. };
  2600. /***********************************
  2601. * PCI Driver Interface functions *
  2602. ***********************************/
  2603. #define FAIL(err, fmt, args...) \
  2604. do { \
  2605. PRINT_G(KERN_ERR, fmt , ## args); \
  2606. ohci1394_pci_remove(dev); \
  2607. return err; \
  2608. } while (0)
  2609. static int __devinit ohci1394_pci_probe(struct pci_dev *dev,
  2610. const struct pci_device_id *ent)
  2611. {
  2612. static int version_printed = 0;
  2613. struct hpsb_host *host;
  2614. struct ti_ohci *ohci; /* shortcut to currently handled device */
  2615. unsigned long ohci_base;
  2616. if (version_printed++ == 0)
  2617. PRINT_G(KERN_INFO, "%s", version);
  2618. if (pci_enable_device(dev))
  2619. FAIL(-ENXIO, "Failed to enable OHCI hardware");
  2620. pci_set_master(dev);
  2621. host = hpsb_alloc_host(&ohci1394_driver, sizeof(struct ti_ohci), &dev->dev);
  2622. if (!host) FAIL(-ENOMEM, "Failed to allocate host structure");
  2623. ohci = host->hostdata;
  2624. ohci->dev = dev;
  2625. ohci->host = host;
  2626. ohci->init_state = OHCI_INIT_ALLOC_HOST;
  2627. host->pdev = dev;
  2628. pci_set_drvdata(dev, ohci);
  2629. /* We don't want hardware swapping */
  2630. pci_write_config_dword(dev, OHCI1394_PCI_HCI_Control, 0);
  2631. /* Some oddball Apple controllers do not order the selfid
  2632. * properly, so we make up for it here. */
  2633. #ifndef __LITTLE_ENDIAN
  2634. /* XXX: Need a better way to check this. I'm wondering if we can
  2635. * read the values of the OHCI1394_PCI_HCI_Control and the
  2636. * noByteSwapData registers to see if they were not cleared to
  2637. * zero. Should this work? Obviously it's not defined what these
  2638. * registers will read when they aren't supported. Bleh! */
  2639. if (dev->vendor == PCI_VENDOR_ID_APPLE &&
  2640. dev->device == PCI_DEVICE_ID_APPLE_UNI_N_FW) {
  2641. ohci->no_swap_incoming = 1;
  2642. ohci->selfid_swap = 0;
  2643. } else
  2644. ohci->selfid_swap = 1;
  2645. #endif
  2646. #ifndef PCI_DEVICE_ID_NVIDIA_NFORCE2_FW
  2647. #define PCI_DEVICE_ID_NVIDIA_NFORCE2_FW 0x006e
  2648. #endif
  2649. /* These chipsets require a bit of extra care when checking after
  2650. * a busreset. */
  2651. if ((dev->vendor == PCI_VENDOR_ID_APPLE &&
  2652. dev->device == PCI_DEVICE_ID_APPLE_UNI_N_FW) ||
  2653. (dev->vendor == PCI_VENDOR_ID_NVIDIA &&
  2654. dev->device == PCI_DEVICE_ID_NVIDIA_NFORCE2_FW))
  2655. ohci->check_busreset = 1;
  2656. /* We hardwire the MMIO length, since some CardBus adaptors
  2657. * fail to report the right length. Anyway, the ohci spec
  2658. * clearly says it's 2kb, so this shouldn't be a problem. */
  2659. ohci_base = pci_resource_start(dev, 0);
  2660. if (pci_resource_len(dev, 0) != OHCI1394_REGISTER_SIZE)
  2661. PRINT(KERN_WARNING, "Unexpected PCI resource length of %lx!",
  2662. pci_resource_len(dev, 0));
  2663. /* Seems PCMCIA handles this internally. Not sure why. Seems
  2664. * pretty bogus to force a driver to special case this. */
  2665. #ifndef PCMCIA
  2666. if (!request_mem_region (ohci_base, OHCI1394_REGISTER_SIZE, OHCI1394_DRIVER_NAME))
  2667. FAIL(-ENOMEM, "MMIO resource (0x%lx - 0x%lx) unavailable",
  2668. ohci_base, ohci_base + OHCI1394_REGISTER_SIZE);
  2669. #endif
  2670. ohci->init_state = OHCI_INIT_HAVE_MEM_REGION;
  2671. ohci->registers = ioremap(ohci_base, OHCI1394_REGISTER_SIZE);
  2672. if (ohci->registers == NULL)
  2673. FAIL(-ENXIO, "Failed to remap registers - card not accessible");
  2674. ohci->init_state = OHCI_INIT_HAVE_IOMAPPING;
  2675. DBGMSG("Remapped memory spaces reg 0x%p", ohci->registers);
  2676. /* csr_config rom allocation */
  2677. ohci->csr_config_rom_cpu =
  2678. pci_alloc_consistent(ohci->dev, OHCI_CONFIG_ROM_LEN,
  2679. &ohci->csr_config_rom_bus);
  2680. OHCI_DMA_ALLOC("consistent csr_config_rom");
  2681. if (ohci->csr_config_rom_cpu == NULL)
  2682. FAIL(-ENOMEM, "Failed to allocate buffer config rom");
  2683. ohci->init_state = OHCI_INIT_HAVE_CONFIG_ROM_BUFFER;
  2684. /* self-id dma buffer allocation */
  2685. ohci->selfid_buf_cpu =
  2686. pci_alloc_consistent(ohci->dev, OHCI1394_SI_DMA_BUF_SIZE,
  2687. &ohci->selfid_buf_bus);
  2688. OHCI_DMA_ALLOC("consistent selfid_buf");
  2689. if (ohci->selfid_buf_cpu == NULL)
  2690. FAIL(-ENOMEM, "Failed to allocate DMA buffer for self-id packets");
  2691. ohci->init_state = OHCI_INIT_HAVE_SELFID_BUFFER;
  2692. if ((unsigned long)ohci->selfid_buf_cpu & 0x1fff)
  2693. PRINT(KERN_INFO, "SelfID buffer %p is not aligned on "
  2694. "8Kb boundary... may cause problems on some CXD3222 chip",
  2695. ohci->selfid_buf_cpu);
  2696. /* No self-id errors at startup */
  2697. ohci->self_id_errors = 0;
  2698. ohci->init_state = OHCI_INIT_HAVE_TXRX_BUFFERS__MAYBE;
  2699. /* AR DMA request context allocation */
  2700. if (alloc_dma_rcv_ctx(ohci, &ohci->ar_req_context,
  2701. DMA_CTX_ASYNC_REQ, 0, AR_REQ_NUM_DESC,
  2702. AR_REQ_BUF_SIZE, AR_REQ_SPLIT_BUF_SIZE,
  2703. OHCI1394_AsReqRcvContextBase) < 0)
  2704. FAIL(-ENOMEM, "Failed to allocate AR Req context");
  2705. /* AR DMA response context allocation */
  2706. if (alloc_dma_rcv_ctx(ohci, &ohci->ar_resp_context,
  2707. DMA_CTX_ASYNC_RESP, 0, AR_RESP_NUM_DESC,
  2708. AR_RESP_BUF_SIZE, AR_RESP_SPLIT_BUF_SIZE,
  2709. OHCI1394_AsRspRcvContextBase) < 0)
  2710. FAIL(-ENOMEM, "Failed to allocate AR Resp context");
  2711. /* AT DMA request context */
  2712. if (alloc_dma_trm_ctx(ohci, &ohci->at_req_context,
  2713. DMA_CTX_ASYNC_REQ, 0, AT_REQ_NUM_DESC,
  2714. OHCI1394_AsReqTrContextBase) < 0)
  2715. FAIL(-ENOMEM, "Failed to allocate AT Req context");
  2716. /* AT DMA response context */
  2717. if (alloc_dma_trm_ctx(ohci, &ohci->at_resp_context,
  2718. DMA_CTX_ASYNC_RESP, 1, AT_RESP_NUM_DESC,
  2719. OHCI1394_AsRspTrContextBase) < 0)
  2720. FAIL(-ENOMEM, "Failed to allocate AT Resp context");
  2721. /* Start off with a soft reset, to clear everything to a sane
  2722. * state. */
  2723. ohci_soft_reset(ohci);
  2724. /* Now enable LPS, which we need in order to start accessing
  2725. * most of the registers. In fact, on some cards (ALI M5251),
  2726. * accessing registers in the SClk domain without LPS enabled
  2727. * will lock up the machine. Wait 50msec to make sure we have
  2728. * full link enabled. */
  2729. reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_LPS);
  2730. /* Disable and clear interrupts */
  2731. reg_write(ohci, OHCI1394_IntEventClear, 0xffffffff);
  2732. reg_write(ohci, OHCI1394_IntMaskClear, 0xffffffff);
  2733. mdelay(50);
  2734. /* Determine the number of available IR and IT contexts. */
  2735. ohci->nb_iso_rcv_ctx =
  2736. get_nb_iso_ctx(ohci, OHCI1394_IsoRecvIntMaskSet);
  2737. DBGMSG("%d iso receive contexts available",
  2738. ohci->nb_iso_rcv_ctx);
  2739. ohci->nb_iso_xmit_ctx =
  2740. get_nb_iso_ctx(ohci, OHCI1394_IsoXmitIntMaskSet);
  2741. DBGMSG("%d iso transmit contexts available",
  2742. ohci->nb_iso_xmit_ctx);
  2743. /* Set the usage bits for non-existent contexts so they can't
  2744. * be allocated */
  2745. ohci->ir_ctx_usage = ~0 << ohci->nb_iso_rcv_ctx;
  2746. ohci->it_ctx_usage = ~0 << ohci->nb_iso_xmit_ctx;
  2747. INIT_LIST_HEAD(&ohci->iso_tasklet_list);
  2748. spin_lock_init(&ohci->iso_tasklet_list_lock);
  2749. ohci->ISO_channel_usage = 0;
  2750. spin_lock_init(&ohci->IR_channel_lock);
  2751. /* Allocate the IR DMA context right here so we don't have
  2752. * to do it in interrupt path - note that this doesn't
  2753. * waste much memory and avoids the jugglery required to
  2754. * allocate it in IRQ path. */
  2755. if (alloc_dma_rcv_ctx(ohci, &ohci->ir_legacy_context,
  2756. DMA_CTX_ISO, 0, IR_NUM_DESC,
  2757. IR_BUF_SIZE, IR_SPLIT_BUF_SIZE,
  2758. OHCI1394_IsoRcvContextBase) < 0) {
  2759. FAIL(-ENOMEM, "Cannot allocate IR Legacy DMA context");
  2760. }
  2761. /* We hopefully don't have to pre-allocate IT DMA like we did
  2762. * for IR DMA above. Allocate it on-demand and mark inactive. */
  2763. ohci->it_legacy_context.ohci = NULL;
  2764. if (request_irq(dev->irq, ohci_irq_handler, SA_SHIRQ,
  2765. OHCI1394_DRIVER_NAME, ohci))
  2766. FAIL(-ENOMEM, "Failed to allocate shared interrupt %d", dev->irq);
  2767. ohci->init_state = OHCI_INIT_HAVE_IRQ;
  2768. ohci_initialize(ohci);
  2769. /* Set certain csr values */
  2770. host->csr.guid_hi = reg_read(ohci, OHCI1394_GUIDHi);
  2771. host->csr.guid_lo = reg_read(ohci, OHCI1394_GUIDLo);
  2772. host->csr.cyc_clk_acc = 100; /* how do we determine clk accuracy? */
  2773. host->csr.max_rec = (reg_read(ohci, OHCI1394_BusOptions) >> 12) & 0xf;
  2774. host->csr.lnk_spd = reg_read(ohci, OHCI1394_BusOptions) & 0x7;
  2775. /* Tell the highlevel this host is ready */
  2776. if (hpsb_add_host(host))
  2777. FAIL(-ENOMEM, "Failed to register host with highlevel");
  2778. ohci->init_state = OHCI_INIT_DONE;
  2779. return 0;
  2780. #undef FAIL
  2781. }
  2782. static void ohci1394_pci_remove(struct pci_dev *pdev)
  2783. {
  2784. struct ti_ohci *ohci;
  2785. struct device *dev;
  2786. ohci = pci_get_drvdata(pdev);
  2787. if (!ohci)
  2788. return;
  2789. dev = get_device(&ohci->host->device);
  2790. switch (ohci->init_state) {
  2791. case OHCI_INIT_DONE:
  2792. stop_dma_rcv_ctx(&ohci->ir_legacy_context);
  2793. hpsb_remove_host(ohci->host);
  2794. /* Clear out BUS Options */
  2795. reg_write(ohci, OHCI1394_ConfigROMhdr, 0);
  2796. reg_write(ohci, OHCI1394_BusOptions,
  2797. (reg_read(ohci, OHCI1394_BusOptions) & 0x0000f007) |
  2798. 0x00ff0000);
  2799. memset(ohci->csr_config_rom_cpu, 0, OHCI_CONFIG_ROM_LEN);
  2800. case OHCI_INIT_HAVE_IRQ:
  2801. /* Clear interrupt registers */
  2802. reg_write(ohci, OHCI1394_IntMaskClear, 0xffffffff);
  2803. reg_write(ohci, OHCI1394_IntEventClear, 0xffffffff);
  2804. reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 0xffffffff);
  2805. reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 0xffffffff);
  2806. reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 0xffffffff);
  2807. reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 0xffffffff);
  2808. /* Disable IRM Contender */
  2809. set_phy_reg(ohci, 4, ~0xc0 & get_phy_reg(ohci, 4));
  2810. /* Clear link control register */
  2811. reg_write(ohci, OHCI1394_LinkControlClear, 0xffffffff);
  2812. /* Let all other nodes know to ignore us */
  2813. ohci_devctl(ohci->host, RESET_BUS, LONG_RESET_NO_FORCE_ROOT);
  2814. /* Soft reset before we start - this disables
  2815. * interrupts and clears linkEnable and LPS. */
  2816. ohci_soft_reset(ohci);
  2817. free_irq(ohci->dev->irq, ohci);
  2818. case OHCI_INIT_HAVE_TXRX_BUFFERS__MAYBE:
  2819. /* The ohci_soft_reset() stops all DMA contexts, so we
  2820. * dont need to do this. */
  2821. /* Free AR dma */
  2822. free_dma_rcv_ctx(&ohci->ar_req_context);
  2823. free_dma_rcv_ctx(&ohci->ar_resp_context);
  2824. /* Free AT dma */
  2825. free_dma_trm_ctx(&ohci->at_req_context);
  2826. free_dma_trm_ctx(&ohci->at_resp_context);
  2827. /* Free IR dma */
  2828. free_dma_rcv_ctx(&ohci->ir_legacy_context);
  2829. /* Free IT dma */
  2830. free_dma_trm_ctx(&ohci->it_legacy_context);
  2831. /* Free IR legacy dma */
  2832. free_dma_rcv_ctx(&ohci->ir_legacy_context);
  2833. case OHCI_INIT_HAVE_SELFID_BUFFER:
  2834. pci_free_consistent(ohci->dev, OHCI1394_SI_DMA_BUF_SIZE,
  2835. ohci->selfid_buf_cpu,
  2836. ohci->selfid_buf_bus);
  2837. OHCI_DMA_FREE("consistent selfid_buf");
  2838. case OHCI_INIT_HAVE_CONFIG_ROM_BUFFER:
  2839. pci_free_consistent(ohci->dev, OHCI_CONFIG_ROM_LEN,
  2840. ohci->csr_config_rom_cpu,
  2841. ohci->csr_config_rom_bus);
  2842. OHCI_DMA_FREE("consistent csr_config_rom");
  2843. case OHCI_INIT_HAVE_IOMAPPING:
  2844. iounmap(ohci->registers);
  2845. case OHCI_INIT_HAVE_MEM_REGION:
  2846. #ifndef PCMCIA
  2847. release_mem_region(pci_resource_start(ohci->dev, 0),
  2848. OHCI1394_REGISTER_SIZE);
  2849. #endif
  2850. #ifdef CONFIG_PPC_PMAC
  2851. /* On UniNorth, power down the cable and turn off the chip
  2852. * clock when the module is removed to save power on
  2853. * laptops. Turning it back ON is done by the arch code when
  2854. * pci_enable_device() is called */
  2855. {
  2856. struct device_node* of_node;
  2857. of_node = pci_device_to_OF_node(ohci->dev);
  2858. if (of_node) {
  2859. pmac_call_feature(PMAC_FTR_1394_ENABLE, of_node, 0, 0);
  2860. pmac_call_feature(PMAC_FTR_1394_CABLE_POWER, of_node, 0, 0);
  2861. }
  2862. }
  2863. #endif /* CONFIG_PPC_PMAC */
  2864. case OHCI_INIT_ALLOC_HOST:
  2865. pci_set_drvdata(ohci->dev, NULL);
  2866. }
  2867. if (dev)
  2868. put_device(dev);
  2869. }
  2870. static int ohci1394_pci_resume (struct pci_dev *pdev)
  2871. {
  2872. #ifdef CONFIG_PMAC_PBOOK
  2873. {
  2874. struct device_node *of_node;
  2875. /* Re-enable 1394 */
  2876. of_node = pci_device_to_OF_node (pdev);
  2877. if (of_node)
  2878. pmac_call_feature (PMAC_FTR_1394_ENABLE, of_node, 0, 1);
  2879. }
  2880. #endif
  2881. pci_enable_device(pdev);
  2882. return 0;
  2883. }
  2884. static int ohci1394_pci_suspend (struct pci_dev *pdev, pm_message_t state)
  2885. {
  2886. #ifdef CONFIG_PMAC_PBOOK
  2887. {
  2888. struct device_node *of_node;
  2889. /* Disable 1394 */
  2890. of_node = pci_device_to_OF_node (pdev);
  2891. if (of_node)
  2892. pmac_call_feature(PMAC_FTR_1394_ENABLE, of_node, 0, 0);
  2893. }
  2894. #endif
  2895. return 0;
  2896. }
  2897. #define PCI_CLASS_FIREWIRE_OHCI ((PCI_CLASS_SERIAL_FIREWIRE << 8) | 0x10)
  2898. static struct pci_device_id ohci1394_pci_tbl[] = {
  2899. {
  2900. .class = PCI_CLASS_FIREWIRE_OHCI,
  2901. .class_mask = PCI_ANY_ID,
  2902. .vendor = PCI_ANY_ID,
  2903. .device = PCI_ANY_ID,
  2904. .subvendor = PCI_ANY_ID,
  2905. .subdevice = PCI_ANY_ID,
  2906. },
  2907. { 0, },
  2908. };
  2909. MODULE_DEVICE_TABLE(pci, ohci1394_pci_tbl);
  2910. static struct pci_driver ohci1394_pci_driver = {
  2911. .name = OHCI1394_DRIVER_NAME,
  2912. .id_table = ohci1394_pci_tbl,
  2913. .probe = ohci1394_pci_probe,
  2914. .remove = ohci1394_pci_remove,
  2915. .resume = ohci1394_pci_resume,
  2916. .suspend = ohci1394_pci_suspend,
  2917. };
  2918. /***********************************
  2919. * OHCI1394 Video Interface *
  2920. ***********************************/
  2921. /* essentially the only purpose of this code is to allow another
  2922. module to hook into ohci's interrupt handler */
  2923. int ohci1394_stop_context(struct ti_ohci *ohci, int reg, char *msg)
  2924. {
  2925. int i=0;
  2926. /* stop the channel program if it's still running */
  2927. reg_write(ohci, reg, 0x8000);
  2928. /* Wait until it effectively stops */
  2929. while (reg_read(ohci, reg) & 0x400) {
  2930. i++;
  2931. if (i>5000) {
  2932. PRINT(KERN_ERR,
  2933. "Runaway loop while stopping context: %s...", msg ? msg : "");
  2934. return 1;
  2935. }
  2936. mb();
  2937. udelay(10);
  2938. }
  2939. if (msg) PRINT(KERN_ERR, "%s: dma prg stopped", msg);
  2940. return 0;
  2941. }
  2942. void ohci1394_init_iso_tasklet(struct ohci1394_iso_tasklet *tasklet, int type,
  2943. void (*func)(unsigned long), unsigned long data)
  2944. {
  2945. tasklet_init(&tasklet->tasklet, func, data);
  2946. tasklet->type = type;
  2947. /* We init the tasklet->link field, so we can list_del() it
  2948. * without worrying whether it was added to the list or not. */
  2949. INIT_LIST_HEAD(&tasklet->link);
  2950. }
  2951. int ohci1394_register_iso_tasklet(struct ti_ohci *ohci,
  2952. struct ohci1394_iso_tasklet *tasklet)
  2953. {
  2954. unsigned long flags, *usage;
  2955. int n, i, r = -EBUSY;
  2956. if (tasklet->type == OHCI_ISO_TRANSMIT) {
  2957. n = ohci->nb_iso_xmit_ctx;
  2958. usage = &ohci->it_ctx_usage;
  2959. }
  2960. else {
  2961. n = ohci->nb_iso_rcv_ctx;
  2962. usage = &ohci->ir_ctx_usage;
  2963. /* only one receive context can be multichannel (OHCI sec 10.4.1) */
  2964. if (tasklet->type == OHCI_ISO_MULTICHANNEL_RECEIVE) {
  2965. if (test_and_set_bit(0, &ohci->ir_multichannel_used)) {
  2966. return r;
  2967. }
  2968. }
  2969. }
  2970. spin_lock_irqsave(&ohci->iso_tasklet_list_lock, flags);
  2971. for (i = 0; i < n; i++)
  2972. if (!test_and_set_bit(i, usage)) {
  2973. tasklet->context = i;
  2974. list_add_tail(&tasklet->link, &ohci->iso_tasklet_list);
  2975. r = 0;
  2976. break;
  2977. }
  2978. spin_unlock_irqrestore(&ohci->iso_tasklet_list_lock, flags);
  2979. return r;
  2980. }
  2981. void ohci1394_unregister_iso_tasklet(struct ti_ohci *ohci,
  2982. struct ohci1394_iso_tasklet *tasklet)
  2983. {
  2984. unsigned long flags;
  2985. tasklet_kill(&tasklet->tasklet);
  2986. spin_lock_irqsave(&ohci->iso_tasklet_list_lock, flags);
  2987. if (tasklet->type == OHCI_ISO_TRANSMIT)
  2988. clear_bit(tasklet->context, &ohci->it_ctx_usage);
  2989. else {
  2990. clear_bit(tasklet->context, &ohci->ir_ctx_usage);
  2991. if (tasklet->type == OHCI_ISO_MULTICHANNEL_RECEIVE) {
  2992. clear_bit(0, &ohci->ir_multichannel_used);
  2993. }
  2994. }
  2995. list_del(&tasklet->link);
  2996. spin_unlock_irqrestore(&ohci->iso_tasklet_list_lock, flags);
  2997. }
  2998. EXPORT_SYMBOL(ohci1394_stop_context);
  2999. EXPORT_SYMBOL(ohci1394_init_iso_tasklet);
  3000. EXPORT_SYMBOL(ohci1394_register_iso_tasklet);
  3001. EXPORT_SYMBOL(ohci1394_unregister_iso_tasklet);
  3002. /***********************************
  3003. * General module initialization *
  3004. ***********************************/
  3005. MODULE_AUTHOR("Sebastien Rougeaux <sebastien.rougeaux@anu.edu.au>");
  3006. MODULE_DESCRIPTION("Driver for PCI OHCI IEEE-1394 controllers");
  3007. MODULE_LICENSE("GPL");
  3008. static void __exit ohci1394_cleanup (void)
  3009. {
  3010. pci_unregister_driver(&ohci1394_pci_driver);
  3011. }
  3012. static int __init ohci1394_init(void)
  3013. {
  3014. return pci_register_driver(&ohci1394_pci_driver);
  3015. }
  3016. module_init(ohci1394_init);
  3017. module_exit(ohci1394_cleanup);