pcilynx.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /*
  2. * pcilynx.c - Texas Instruments PCILynx driver
  3. * Copyright (C) 1999,2000 Andreas Bombe <andreas.bombe@munich.netsurf.de>,
  4. * Stephan Linz <linz@mazet.de>
  5. * Manfred Weihs <weihs@ict.tuwien.ac.at>
  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. * Contributions:
  23. *
  24. * Manfred Weihs <weihs@ict.tuwien.ac.at>
  25. * reading bus info block (containing GUID) from serial
  26. * eeprom via i2c and storing it in config ROM
  27. * Reworked code for initiating bus resets
  28. * (long, short, with or without hold-off)
  29. * Enhancements in async and iso send code
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/wait.h>
  35. #include <linux/errno.h>
  36. #include <linux/module.h>
  37. #include <linux/moduleparam.h>
  38. #include <linux/init.h>
  39. #include <linux/pci.h>
  40. #include <linux/fs.h>
  41. #include <linux/poll.h>
  42. #include <linux/kdev_t.h>
  43. #include <linux/dma-mapping.h>
  44. #include <asm/byteorder.h>
  45. #include <asm/atomic.h>
  46. #include <asm/io.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/irq.h>
  49. #include "csr1212.h"
  50. #include "ieee1394.h"
  51. #include "ieee1394_types.h"
  52. #include "hosts.h"
  53. #include "ieee1394_core.h"
  54. #include "highlevel.h"
  55. #include "pcilynx.h"
  56. #include <linux/i2c.h>
  57. #include <linux/i2c-algo-bit.h>
  58. /* print general (card independent) information */
  59. #define PRINT_G(level, fmt, args...) printk(level "pcilynx: " fmt "\n" , ## args)
  60. /* print card specific information */
  61. #define PRINT(level, card, fmt, args...) printk(level "pcilynx%d: " fmt "\n" , card , ## args)
  62. #ifdef CONFIG_IEEE1394_VERBOSEDEBUG
  63. #define PRINT_GD(level, fmt, args...) printk(level "pcilynx: " fmt "\n" , ## args)
  64. #define PRINTD(level, card, fmt, args...) printk(level "pcilynx%d: " fmt "\n" , card , ## args)
  65. #else
  66. #define PRINT_GD(level, fmt, args...) do {} while (0)
  67. #define PRINTD(level, card, fmt, args...) do {} while (0)
  68. #endif
  69. /* Module Parameters */
  70. static int skip_eeprom;
  71. module_param(skip_eeprom, int, 0444);
  72. MODULE_PARM_DESC(skip_eeprom, "Use generic bus info block instead of serial eeprom (default = 0).");
  73. static struct hpsb_host_driver lynx_driver;
  74. static unsigned int card_id;
  75. /*
  76. * I2C stuff
  77. */
  78. /* the i2c stuff was inspired by i2c-philips-par.c */
  79. static void bit_setscl(void *data, int state)
  80. {
  81. if (state) {
  82. ((struct ti_lynx *) data)->i2c_driven_state |= 0x00000040;
  83. } else {
  84. ((struct ti_lynx *) data)->i2c_driven_state &= ~0x00000040;
  85. }
  86. reg_write((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL, ((struct ti_lynx *) data)->i2c_driven_state);
  87. }
  88. static void bit_setsda(void *data, int state)
  89. {
  90. if (state) {
  91. ((struct ti_lynx *) data)->i2c_driven_state |= 0x00000010;
  92. } else {
  93. ((struct ti_lynx *) data)->i2c_driven_state &= ~0x00000010;
  94. }
  95. reg_write((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL, ((struct ti_lynx *) data)->i2c_driven_state);
  96. }
  97. static int bit_getscl(void *data)
  98. {
  99. return reg_read((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL) & 0x00000040;
  100. }
  101. static int bit_getsda(void *data)
  102. {
  103. return reg_read((struct ti_lynx *) data, SERIAL_EEPROM_CONTROL) & 0x00000010;
  104. }
  105. static int bit_reg(struct i2c_client *client)
  106. {
  107. return 0;
  108. }
  109. static int bit_unreg(struct i2c_client *client)
  110. {
  111. return 0;
  112. }
  113. static struct i2c_algo_bit_data bit_data = {
  114. .setsda = bit_setsda,
  115. .setscl = bit_setscl,
  116. .getsda = bit_getsda,
  117. .getscl = bit_getscl,
  118. .udelay = 5,
  119. .timeout = 100,
  120. };
  121. static struct i2c_adapter bit_ops = {
  122. .id = 0xAA, //FIXME: probably we should get an id in i2c-id.h
  123. .client_register = bit_reg,
  124. .client_unregister = bit_unreg,
  125. .name = "PCILynx I2C",
  126. };
  127. /*
  128. * PCL handling functions.
  129. */
  130. static pcl_t alloc_pcl(struct ti_lynx *lynx)
  131. {
  132. u8 m;
  133. int i, j;
  134. spin_lock(&lynx->lock);
  135. /* FIXME - use ffz() to make this readable */
  136. for (i = 0; i < (LOCALRAM_SIZE / 1024); i++) {
  137. m = lynx->pcl_bmap[i];
  138. for (j = 0; j < 8; j++) {
  139. if (m & 1<<j) {
  140. continue;
  141. }
  142. m |= 1<<j;
  143. lynx->pcl_bmap[i] = m;
  144. spin_unlock(&lynx->lock);
  145. return 8 * i + j;
  146. }
  147. }
  148. spin_unlock(&lynx->lock);
  149. return -1;
  150. }
  151. #if 0
  152. static void free_pcl(struct ti_lynx *lynx, pcl_t pclid)
  153. {
  154. int off, bit;
  155. off = pclid / 8;
  156. bit = pclid % 8;
  157. if (pclid < 0) {
  158. return;
  159. }
  160. spin_lock(&lynx->lock);
  161. if (lynx->pcl_bmap[off] & 1<<bit) {
  162. lynx->pcl_bmap[off] &= ~(1<<bit);
  163. } else {
  164. PRINT(KERN_ERR, lynx->id,
  165. "attempted to free unallocated PCL %d", pclid);
  166. }
  167. spin_unlock(&lynx->lock);
  168. }
  169. /* functions useful for debugging */
  170. static void pretty_print_pcl(const struct ti_pcl *pcl)
  171. {
  172. int i;
  173. printk("PCL next %08x, userdata %08x, status %08x, remtrans %08x, nextbuf %08x\n",
  174. pcl->next, pcl->user_data, pcl->pcl_status,
  175. pcl->remaining_transfer_count, pcl->next_data_buffer);
  176. printk("PCL");
  177. for (i=0; i<13; i++) {
  178. printk(" c%x:%08x d%x:%08x",
  179. i, pcl->buffer[i].control, i, pcl->buffer[i].pointer);
  180. if (!(i & 0x3) && (i != 12)) printk("\nPCL");
  181. }
  182. printk("\n");
  183. }
  184. static void print_pcl(const struct ti_lynx *lynx, pcl_t pclid)
  185. {
  186. struct ti_pcl pcl;
  187. get_pcl(lynx, pclid, &pcl);
  188. pretty_print_pcl(&pcl);
  189. }
  190. #endif
  191. /***********************************
  192. * IEEE-1394 functionality section *
  193. ***********************************/
  194. static int get_phy_reg(struct ti_lynx *lynx, int addr)
  195. {
  196. int retval;
  197. int i = 0;
  198. unsigned long flags;
  199. if (addr > 15) {
  200. PRINT(KERN_ERR, lynx->id,
  201. "%s: PHY register address %d out of range",
  202. __FUNCTION__, addr);
  203. return -1;
  204. }
  205. spin_lock_irqsave(&lynx->phy_reg_lock, flags);
  206. reg_write(lynx, LINK_PHY, LINK_PHY_READ | LINK_PHY_ADDR(addr));
  207. do {
  208. retval = reg_read(lynx, LINK_PHY);
  209. if (i > 10000) {
  210. PRINT(KERN_ERR, lynx->id, "%s: runaway loop, aborting",
  211. __FUNCTION__);
  212. retval = -1;
  213. break;
  214. }
  215. i++;
  216. } while ((retval & 0xf00) != LINK_PHY_RADDR(addr));
  217. reg_write(lynx, LINK_INT_STATUS, LINK_INT_PHY_REG_RCVD);
  218. spin_unlock_irqrestore(&lynx->phy_reg_lock, flags);
  219. if (retval != -1) {
  220. return retval & 0xff;
  221. } else {
  222. return -1;
  223. }
  224. }
  225. static int set_phy_reg(struct ti_lynx *lynx, int addr, int val)
  226. {
  227. unsigned long flags;
  228. if (addr > 15) {
  229. PRINT(KERN_ERR, lynx->id,
  230. "%s: PHY register address %d out of range", __FUNCTION__, addr);
  231. return -1;
  232. }
  233. if (val > 0xff) {
  234. PRINT(KERN_ERR, lynx->id,
  235. "%s: PHY register value %d out of range", __FUNCTION__, val);
  236. return -1;
  237. }
  238. spin_lock_irqsave(&lynx->phy_reg_lock, flags);
  239. reg_write(lynx, LINK_PHY, LINK_PHY_WRITE | LINK_PHY_ADDR(addr)
  240. | LINK_PHY_WDATA(val));
  241. spin_unlock_irqrestore(&lynx->phy_reg_lock, flags);
  242. return 0;
  243. }
  244. static int sel_phy_reg_page(struct ti_lynx *lynx, int page)
  245. {
  246. int reg;
  247. if (page > 7) {
  248. PRINT(KERN_ERR, lynx->id,
  249. "%s: PHY page %d out of range", __FUNCTION__, page);
  250. return -1;
  251. }
  252. reg = get_phy_reg(lynx, 7);
  253. if (reg != -1) {
  254. reg &= 0x1f;
  255. reg |= (page << 5);
  256. set_phy_reg(lynx, 7, reg);
  257. return 0;
  258. } else {
  259. return -1;
  260. }
  261. }
  262. #if 0 /* not needed at this time */
  263. static int sel_phy_reg_port(struct ti_lynx *lynx, int port)
  264. {
  265. int reg;
  266. if (port > 15) {
  267. PRINT(KERN_ERR, lynx->id,
  268. "%s: PHY port %d out of range", __FUNCTION__, port);
  269. return -1;
  270. }
  271. reg = get_phy_reg(lynx, 7);
  272. if (reg != -1) {
  273. reg &= 0xf0;
  274. reg |= port;
  275. set_phy_reg(lynx, 7, reg);
  276. return 0;
  277. } else {
  278. return -1;
  279. }
  280. }
  281. #endif
  282. static u32 get_phy_vendorid(struct ti_lynx *lynx)
  283. {
  284. u32 pvid = 0;
  285. sel_phy_reg_page(lynx, 1);
  286. pvid |= (get_phy_reg(lynx, 10) << 16);
  287. pvid |= (get_phy_reg(lynx, 11) << 8);
  288. pvid |= get_phy_reg(lynx, 12);
  289. PRINT(KERN_INFO, lynx->id, "PHY vendor id 0x%06x", pvid);
  290. return pvid;
  291. }
  292. static u32 get_phy_productid(struct ti_lynx *lynx)
  293. {
  294. u32 id = 0;
  295. sel_phy_reg_page(lynx, 1);
  296. id |= (get_phy_reg(lynx, 13) << 16);
  297. id |= (get_phy_reg(lynx, 14) << 8);
  298. id |= get_phy_reg(lynx, 15);
  299. PRINT(KERN_INFO, lynx->id, "PHY product id 0x%06x", id);
  300. return id;
  301. }
  302. static quadlet_t generate_own_selfid(struct ti_lynx *lynx,
  303. struct hpsb_host *host)
  304. {
  305. quadlet_t lsid;
  306. char phyreg[7];
  307. int i;
  308. phyreg[0] = lynx->phy_reg0;
  309. for (i = 1; i < 7; i++) {
  310. phyreg[i] = get_phy_reg(lynx, i);
  311. }
  312. /* FIXME? We assume a TSB21LV03A phy here. This code doesn't support
  313. more than 3 ports on the PHY anyway. */
  314. lsid = 0x80400000 | ((phyreg[0] & 0xfc) << 22);
  315. lsid |= (phyreg[1] & 0x3f) << 16; /* gap count */
  316. lsid |= (phyreg[2] & 0xc0) << 8; /* max speed */
  317. if (!hpsb_disable_irm)
  318. lsid |= (phyreg[6] & 0x01) << 11; /* contender (phy dependent) */
  319. /* lsid |= 1 << 11; *//* set contender (hack) */
  320. lsid |= (phyreg[6] & 0x10) >> 3; /* initiated reset */
  321. for (i = 0; i < (phyreg[2] & 0xf); i++) { /* ports */
  322. if (phyreg[3 + i] & 0x4) {
  323. lsid |= (((phyreg[3 + i] & 0x8) | 0x10) >> 3)
  324. << (6 - i*2);
  325. } else {
  326. lsid |= 1 << (6 - i*2);
  327. }
  328. }
  329. cpu_to_be32s(&lsid);
  330. PRINT(KERN_DEBUG, lynx->id, "generated own selfid 0x%x", lsid);
  331. return lsid;
  332. }
  333. static void handle_selfid(struct ti_lynx *lynx, struct hpsb_host *host)
  334. {
  335. quadlet_t *q = lynx->rcv_page;
  336. int phyid, isroot, size;
  337. quadlet_t lsid = 0;
  338. int i;
  339. if (lynx->phy_reg0 == -1 || lynx->selfid_size == -1) return;
  340. size = lynx->selfid_size;
  341. phyid = lynx->phy_reg0;
  342. i = (size > 16 ? 16 : size) / 4 - 1;
  343. while (i >= 0) {
  344. cpu_to_be32s(&q[i]);
  345. i--;
  346. }
  347. if (!lynx->phyic.reg_1394a) {
  348. lsid = generate_own_selfid(lynx, host);
  349. }
  350. isroot = (phyid & 2) != 0;
  351. phyid >>= 2;
  352. PRINT(KERN_INFO, lynx->id, "SelfID process finished (phyid %d, %s)",
  353. phyid, (isroot ? "root" : "not root"));
  354. reg_write(lynx, LINK_ID, (0xffc0 | phyid) << 16);
  355. if (!lynx->phyic.reg_1394a && !size) {
  356. hpsb_selfid_received(host, lsid);
  357. }
  358. while (size > 0) {
  359. struct selfid *sid = (struct selfid *)q;
  360. if (!lynx->phyic.reg_1394a && !sid->extended
  361. && (sid->phy_id == (phyid + 1))) {
  362. hpsb_selfid_received(host, lsid);
  363. }
  364. if (q[0] == ~q[1]) {
  365. PRINT(KERN_DEBUG, lynx->id, "SelfID packet 0x%x rcvd",
  366. q[0]);
  367. hpsb_selfid_received(host, q[0]);
  368. } else {
  369. PRINT(KERN_INFO, lynx->id,
  370. "inconsistent selfid 0x%x/0x%x", q[0], q[1]);
  371. }
  372. q += 2;
  373. size -= 8;
  374. }
  375. if (!lynx->phyic.reg_1394a && isroot && phyid != 0) {
  376. hpsb_selfid_received(host, lsid);
  377. }
  378. hpsb_selfid_complete(host, phyid, isroot);
  379. if (host->in_bus_reset) return; /* in bus reset again */
  380. if (isroot) reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_CYCMASTER); //FIXME: I do not think, we need this here
  381. reg_set_bits(lynx, LINK_CONTROL,
  382. LINK_CONTROL_RCV_CMP_VALID | LINK_CONTROL_TX_ASYNC_EN
  383. | LINK_CONTROL_RX_ASYNC_EN | LINK_CONTROL_CYCTIMEREN);
  384. }
  385. /* This must be called with the respective queue_lock held. */
  386. static void send_next(struct ti_lynx *lynx, int what)
  387. {
  388. struct ti_pcl pcl;
  389. struct lynx_send_data *d;
  390. struct hpsb_packet *packet;
  391. d = (what == hpsb_iso ? &lynx->iso_send : &lynx->async);
  392. if (!list_empty(&d->pcl_queue)) {
  393. PRINT(KERN_ERR, lynx->id, "trying to queue a new packet in nonempty fifo");
  394. BUG();
  395. }
  396. packet = driver_packet(d->queue.next);
  397. list_move_tail(&packet->driver_list, &d->pcl_queue);
  398. d->header_dma = pci_map_single(lynx->dev, packet->header,
  399. packet->header_size, PCI_DMA_TODEVICE);
  400. if (packet->data_size) {
  401. d->data_dma = pci_map_single(lynx->dev, packet->data,
  402. packet->data_size,
  403. PCI_DMA_TODEVICE);
  404. } else {
  405. d->data_dma = 0;
  406. }
  407. pcl.next = PCL_NEXT_INVALID;
  408. pcl.async_error_next = PCL_NEXT_INVALID;
  409. pcl.pcl_status = 0;
  410. pcl.buffer[0].control = packet->speed_code << 14 | packet->header_size;
  411. #ifndef __BIG_ENDIAN
  412. pcl.buffer[0].control |= PCL_BIGENDIAN;
  413. #endif
  414. pcl.buffer[0].pointer = d->header_dma;
  415. pcl.buffer[1].control = PCL_LAST_BUFF | packet->data_size;
  416. pcl.buffer[1].pointer = d->data_dma;
  417. switch (packet->type) {
  418. case hpsb_async:
  419. pcl.buffer[0].control |= PCL_CMD_XMT;
  420. break;
  421. case hpsb_iso:
  422. pcl.buffer[0].control |= PCL_CMD_XMT | PCL_ISOMODE;
  423. break;
  424. case hpsb_raw:
  425. pcl.buffer[0].control |= PCL_CMD_UNFXMT;
  426. break;
  427. }
  428. put_pcl(lynx, d->pcl, &pcl);
  429. run_pcl(lynx, d->pcl_start, d->channel);
  430. }
  431. /* called from subsystem core */
  432. static int lynx_transmit(struct hpsb_host *host, struct hpsb_packet *packet)
  433. {
  434. struct ti_lynx *lynx = host->hostdata;
  435. struct lynx_send_data *d;
  436. unsigned long flags;
  437. if (packet->data_size >= 4096) {
  438. PRINT(KERN_ERR, lynx->id, "transmit packet data too big (%Zd)",
  439. packet->data_size);
  440. return -EOVERFLOW;
  441. }
  442. switch (packet->type) {
  443. case hpsb_async:
  444. case hpsb_raw:
  445. d = &lynx->async;
  446. break;
  447. case hpsb_iso:
  448. d = &lynx->iso_send;
  449. break;
  450. default:
  451. PRINT(KERN_ERR, lynx->id, "invalid packet type %d",
  452. packet->type);
  453. return -EINVAL;
  454. }
  455. if (packet->tcode == TCODE_WRITEQ
  456. || packet->tcode == TCODE_READQ_RESPONSE) {
  457. cpu_to_be32s(&packet->header[3]);
  458. }
  459. spin_lock_irqsave(&d->queue_lock, flags);
  460. list_add_tail(&packet->driver_list, &d->queue);
  461. if (list_empty(&d->pcl_queue))
  462. send_next(lynx, packet->type);
  463. spin_unlock_irqrestore(&d->queue_lock, flags);
  464. return 0;
  465. }
  466. /* called from subsystem core */
  467. static int lynx_devctl(struct hpsb_host *host, enum devctl_cmd cmd, int arg)
  468. {
  469. struct ti_lynx *lynx = host->hostdata;
  470. int retval = 0;
  471. struct hpsb_packet *packet;
  472. LIST_HEAD(packet_list);
  473. unsigned long flags;
  474. int phy_reg;
  475. switch (cmd) {
  476. case RESET_BUS:
  477. if (reg_read(lynx, LINK_INT_STATUS) & LINK_INT_PHY_BUSRESET) {
  478. retval = 0;
  479. break;
  480. }
  481. switch (arg) {
  482. case SHORT_RESET:
  483. if (lynx->phyic.reg_1394a) {
  484. phy_reg = get_phy_reg(lynx, 5);
  485. if (phy_reg == -1) {
  486. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  487. retval = -1;
  488. break;
  489. }
  490. phy_reg |= 0x40;
  491. PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset) on request");
  492. lynx->selfid_size = -1;
  493. lynx->phy_reg0 = -1;
  494. set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
  495. break;
  496. } else {
  497. PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
  498. /* fall through to long bus reset */
  499. }
  500. case LONG_RESET:
  501. phy_reg = get_phy_reg(lynx, 1);
  502. if (phy_reg == -1) {
  503. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  504. retval = -1;
  505. break;
  506. }
  507. phy_reg |= 0x40;
  508. PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset) on request");
  509. lynx->selfid_size = -1;
  510. lynx->phy_reg0 = -1;
  511. set_phy_reg(lynx, 1, phy_reg); /* clear RHB, set IBR */
  512. break;
  513. case SHORT_RESET_NO_FORCE_ROOT:
  514. if (lynx->phyic.reg_1394a) {
  515. phy_reg = get_phy_reg(lynx, 1);
  516. if (phy_reg == -1) {
  517. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  518. retval = -1;
  519. break;
  520. }
  521. if (phy_reg & 0x80) {
  522. phy_reg &= ~0x80;
  523. set_phy_reg(lynx, 1, phy_reg); /* clear RHB */
  524. }
  525. phy_reg = get_phy_reg(lynx, 5);
  526. if (phy_reg == -1) {
  527. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  528. retval = -1;
  529. break;
  530. }
  531. phy_reg |= 0x40;
  532. PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset, no force_root) on request");
  533. lynx->selfid_size = -1;
  534. lynx->phy_reg0 = -1;
  535. set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
  536. break;
  537. } else {
  538. PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
  539. /* fall through to long bus reset */
  540. }
  541. case LONG_RESET_NO_FORCE_ROOT:
  542. phy_reg = get_phy_reg(lynx, 1);
  543. if (phy_reg == -1) {
  544. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  545. retval = -1;
  546. break;
  547. }
  548. phy_reg &= ~0x80;
  549. phy_reg |= 0x40;
  550. PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset, no force_root) on request");
  551. lynx->selfid_size = -1;
  552. lynx->phy_reg0 = -1;
  553. set_phy_reg(lynx, 1, phy_reg); /* clear RHB, set IBR */
  554. break;
  555. case SHORT_RESET_FORCE_ROOT:
  556. if (lynx->phyic.reg_1394a) {
  557. phy_reg = get_phy_reg(lynx, 1);
  558. if (phy_reg == -1) {
  559. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  560. retval = -1;
  561. break;
  562. }
  563. if (!(phy_reg & 0x80)) {
  564. phy_reg |= 0x80;
  565. set_phy_reg(lynx, 1, phy_reg); /* set RHB */
  566. }
  567. phy_reg = get_phy_reg(lynx, 5);
  568. if (phy_reg == -1) {
  569. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  570. retval = -1;
  571. break;
  572. }
  573. phy_reg |= 0x40;
  574. PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset, force_root set) on request");
  575. lynx->selfid_size = -1;
  576. lynx->phy_reg0 = -1;
  577. set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
  578. break;
  579. } else {
  580. PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
  581. /* fall through to long bus reset */
  582. }
  583. case LONG_RESET_FORCE_ROOT:
  584. phy_reg = get_phy_reg(lynx, 1);
  585. if (phy_reg == -1) {
  586. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  587. retval = -1;
  588. break;
  589. }
  590. phy_reg |= 0xc0;
  591. PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset, force_root set) on request");
  592. lynx->selfid_size = -1;
  593. lynx->phy_reg0 = -1;
  594. set_phy_reg(lynx, 1, phy_reg); /* set IBR and RHB */
  595. break;
  596. default:
  597. PRINT(KERN_ERR, lynx->id, "unknown argument for reset_bus command %d", arg);
  598. retval = -1;
  599. }
  600. break;
  601. case GET_CYCLE_COUNTER:
  602. retval = reg_read(lynx, CYCLE_TIMER);
  603. break;
  604. case SET_CYCLE_COUNTER:
  605. reg_write(lynx, CYCLE_TIMER, arg);
  606. break;
  607. case SET_BUS_ID:
  608. reg_write(lynx, LINK_ID,
  609. (arg << 22) | (reg_read(lynx, LINK_ID) & 0x003f0000));
  610. break;
  611. case ACT_CYCLE_MASTER:
  612. if (arg) {
  613. reg_set_bits(lynx, LINK_CONTROL,
  614. LINK_CONTROL_CYCMASTER);
  615. } else {
  616. reg_clear_bits(lynx, LINK_CONTROL,
  617. LINK_CONTROL_CYCMASTER);
  618. }
  619. break;
  620. case CANCEL_REQUESTS:
  621. spin_lock_irqsave(&lynx->async.queue_lock, flags);
  622. reg_write(lynx, DMA_CHAN_CTRL(CHANNEL_ASYNC_SEND), 0);
  623. list_splice(&lynx->async.queue, &packet_list);
  624. INIT_LIST_HEAD(&lynx->async.queue);
  625. if (list_empty(&lynx->async.pcl_queue)) {
  626. spin_unlock_irqrestore(&lynx->async.queue_lock, flags);
  627. PRINTD(KERN_DEBUG, lynx->id, "no async packet in PCL to cancel");
  628. } else {
  629. struct ti_pcl pcl;
  630. u32 ack;
  631. struct hpsb_packet *packet;
  632. PRINT(KERN_INFO, lynx->id, "cancelling async packet, that was already in PCL");
  633. get_pcl(lynx, lynx->async.pcl, &pcl);
  634. packet = driver_packet(lynx->async.pcl_queue.next);
  635. list_del_init(&packet->driver_list);
  636. pci_unmap_single(lynx->dev, lynx->async.header_dma,
  637. packet->header_size, PCI_DMA_TODEVICE);
  638. if (packet->data_size) {
  639. pci_unmap_single(lynx->dev, lynx->async.data_dma,
  640. packet->data_size, PCI_DMA_TODEVICE);
  641. }
  642. spin_unlock_irqrestore(&lynx->async.queue_lock, flags);
  643. if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
  644. if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
  645. ack = (pcl.pcl_status >> 15) & 0xf;
  646. PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
  647. ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
  648. } else {
  649. ack = (pcl.pcl_status >> 15) & 0xf;
  650. }
  651. } else {
  652. PRINT(KERN_INFO, lynx->id, "async packet was not completed");
  653. ack = ACKX_ABORTED;
  654. }
  655. hpsb_packet_sent(host, packet, ack);
  656. }
  657. while (!list_empty(&packet_list)) {
  658. packet = driver_packet(packet_list.next);
  659. list_del_init(&packet->driver_list);
  660. hpsb_packet_sent(host, packet, ACKX_ABORTED);
  661. }
  662. break;
  663. case ISO_LISTEN_CHANNEL:
  664. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  665. if (lynx->iso_rcv.chan_count++ == 0) {
  666. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
  667. DMA_WORD1_CMP_ENABLE_MASTER);
  668. }
  669. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  670. break;
  671. case ISO_UNLISTEN_CHANNEL:
  672. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  673. if (--lynx->iso_rcv.chan_count == 0) {
  674. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
  675. 0);
  676. }
  677. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  678. break;
  679. default:
  680. PRINT(KERN_ERR, lynx->id, "unknown devctl command %d", cmd);
  681. retval = -1;
  682. }
  683. return retval;
  684. }
  685. /***************************************
  686. * IEEE-1394 functionality section END *
  687. ***************************************/
  688. /********************************************************
  689. * Global stuff (interrupt handler, init/shutdown code) *
  690. ********************************************************/
  691. static irqreturn_t lynx_irq_handler(int irq, void *dev_id)
  692. {
  693. struct ti_lynx *lynx = (struct ti_lynx *)dev_id;
  694. struct hpsb_host *host = lynx->host;
  695. u32 intmask;
  696. u32 linkint;
  697. linkint = reg_read(lynx, LINK_INT_STATUS);
  698. intmask = reg_read(lynx, PCI_INT_STATUS);
  699. if (!(intmask & PCI_INT_INT_PEND))
  700. return IRQ_NONE;
  701. PRINTD(KERN_DEBUG, lynx->id, "interrupt: 0x%08x / 0x%08x", intmask,
  702. linkint);
  703. reg_write(lynx, LINK_INT_STATUS, linkint);
  704. reg_write(lynx, PCI_INT_STATUS, intmask);
  705. if (intmask & PCI_INT_1394) {
  706. if (linkint & LINK_INT_PHY_TIMEOUT) {
  707. PRINT(KERN_INFO, lynx->id, "PHY timeout occurred");
  708. }
  709. if (linkint & LINK_INT_PHY_BUSRESET) {
  710. PRINT(KERN_INFO, lynx->id, "bus reset interrupt");
  711. lynx->selfid_size = -1;
  712. lynx->phy_reg0 = -1;
  713. if (!host->in_bus_reset)
  714. hpsb_bus_reset(host);
  715. }
  716. if (linkint & LINK_INT_PHY_REG_RCVD) {
  717. u32 reg;
  718. spin_lock(&lynx->phy_reg_lock);
  719. reg = reg_read(lynx, LINK_PHY);
  720. spin_unlock(&lynx->phy_reg_lock);
  721. if (!host->in_bus_reset) {
  722. PRINT(KERN_INFO, lynx->id,
  723. "phy reg received without reset");
  724. } else if (reg & 0xf00) {
  725. PRINT(KERN_INFO, lynx->id,
  726. "unsolicited phy reg %d received",
  727. (reg >> 8) & 0xf);
  728. } else {
  729. lynx->phy_reg0 = reg & 0xff;
  730. handle_selfid(lynx, host);
  731. }
  732. }
  733. if (linkint & LINK_INT_ISO_STUCK) {
  734. PRINT(KERN_INFO, lynx->id, "isochronous transmitter stuck");
  735. }
  736. if (linkint & LINK_INT_ASYNC_STUCK) {
  737. PRINT(KERN_INFO, lynx->id, "asynchronous transmitter stuck");
  738. }
  739. if (linkint & LINK_INT_SENT_REJECT) {
  740. PRINT(KERN_INFO, lynx->id, "sent reject");
  741. }
  742. if (linkint & LINK_INT_TX_INVALID_TC) {
  743. PRINT(KERN_INFO, lynx->id, "invalid transaction code");
  744. }
  745. if (linkint & LINK_INT_GRF_OVERFLOW) {
  746. /* flush FIFO if overflow happens during reset */
  747. if (host->in_bus_reset)
  748. reg_write(lynx, FIFO_CONTROL,
  749. FIFO_CONTROL_GRF_FLUSH);
  750. PRINT(KERN_INFO, lynx->id, "GRF overflow");
  751. }
  752. if (linkint & LINK_INT_ITF_UNDERFLOW) {
  753. PRINT(KERN_INFO, lynx->id, "ITF underflow");
  754. }
  755. if (linkint & LINK_INT_ATF_UNDERFLOW) {
  756. PRINT(KERN_INFO, lynx->id, "ATF underflow");
  757. }
  758. }
  759. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ISO_RCV)) {
  760. PRINTD(KERN_DEBUG, lynx->id, "iso receive");
  761. spin_lock(&lynx->iso_rcv.lock);
  762. lynx->iso_rcv.stat[lynx->iso_rcv.next] =
  763. reg_read(lynx, DMA_CHAN_STAT(CHANNEL_ISO_RCV));
  764. lynx->iso_rcv.used++;
  765. lynx->iso_rcv.next = (lynx->iso_rcv.next + 1) % NUM_ISORCV_PCL;
  766. if ((lynx->iso_rcv.next == lynx->iso_rcv.last)
  767. || !lynx->iso_rcv.chan_count) {
  768. PRINTD(KERN_DEBUG, lynx->id, "stopped");
  769. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV), 0);
  770. }
  771. run_sub_pcl(lynx, lynx->iso_rcv.pcl_start, lynx->iso_rcv.next,
  772. CHANNEL_ISO_RCV);
  773. spin_unlock(&lynx->iso_rcv.lock);
  774. tasklet_schedule(&lynx->iso_rcv.tq);
  775. }
  776. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ASYNC_SEND)) {
  777. PRINTD(KERN_DEBUG, lynx->id, "async sent");
  778. spin_lock(&lynx->async.queue_lock);
  779. if (list_empty(&lynx->async.pcl_queue)) {
  780. spin_unlock(&lynx->async.queue_lock);
  781. PRINT(KERN_WARNING, lynx->id, "async dma halted, but no queued packet (maybe it was cancelled)");
  782. } else {
  783. struct ti_pcl pcl;
  784. u32 ack;
  785. struct hpsb_packet *packet;
  786. get_pcl(lynx, lynx->async.pcl, &pcl);
  787. packet = driver_packet(lynx->async.pcl_queue.next);
  788. list_del_init(&packet->driver_list);
  789. pci_unmap_single(lynx->dev, lynx->async.header_dma,
  790. packet->header_size, PCI_DMA_TODEVICE);
  791. if (packet->data_size) {
  792. pci_unmap_single(lynx->dev, lynx->async.data_dma,
  793. packet->data_size, PCI_DMA_TODEVICE);
  794. }
  795. if (!list_empty(&lynx->async.queue)) {
  796. send_next(lynx, hpsb_async);
  797. }
  798. spin_unlock(&lynx->async.queue_lock);
  799. if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
  800. if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
  801. ack = (pcl.pcl_status >> 15) & 0xf;
  802. PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
  803. ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
  804. } else {
  805. ack = (pcl.pcl_status >> 15) & 0xf;
  806. }
  807. } else {
  808. PRINT(KERN_INFO, lynx->id, "async packet was not completed");
  809. ack = ACKX_SEND_ERROR;
  810. }
  811. hpsb_packet_sent(host, packet, ack);
  812. }
  813. }
  814. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ISO_SEND)) {
  815. PRINTD(KERN_DEBUG, lynx->id, "iso sent");
  816. spin_lock(&lynx->iso_send.queue_lock);
  817. if (list_empty(&lynx->iso_send.pcl_queue)) {
  818. spin_unlock(&lynx->iso_send.queue_lock);
  819. PRINT(KERN_ERR, lynx->id, "iso send dma halted, but no queued packet");
  820. } else {
  821. struct ti_pcl pcl;
  822. u32 ack;
  823. struct hpsb_packet *packet;
  824. get_pcl(lynx, lynx->iso_send.pcl, &pcl);
  825. packet = driver_packet(lynx->iso_send.pcl_queue.next);
  826. list_del_init(&packet->driver_list);
  827. pci_unmap_single(lynx->dev, lynx->iso_send.header_dma,
  828. packet->header_size, PCI_DMA_TODEVICE);
  829. if (packet->data_size) {
  830. pci_unmap_single(lynx->dev, lynx->iso_send.data_dma,
  831. packet->data_size, PCI_DMA_TODEVICE);
  832. }
  833. if (!list_empty(&lynx->iso_send.queue)) {
  834. send_next(lynx, hpsb_iso);
  835. }
  836. spin_unlock(&lynx->iso_send.queue_lock);
  837. if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
  838. if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
  839. ack = (pcl.pcl_status >> 15) & 0xf;
  840. PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
  841. ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
  842. } else {
  843. ack = (pcl.pcl_status >> 15) & 0xf;
  844. }
  845. } else {
  846. PRINT(KERN_INFO, lynx->id, "iso send packet was not completed");
  847. ack = ACKX_SEND_ERROR;
  848. }
  849. hpsb_packet_sent(host, packet, ack); //FIXME: maybe we should just use ACK_COMPLETE and ACKX_SEND_ERROR
  850. }
  851. }
  852. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ASYNC_RCV)) {
  853. /* general receive DMA completed */
  854. int stat = reg_read(lynx, DMA_CHAN_STAT(CHANNEL_ASYNC_RCV));
  855. PRINTD(KERN_DEBUG, lynx->id, "received packet size %d",
  856. stat & 0x1fff);
  857. if (stat & DMA_CHAN_STAT_SELFID) {
  858. lynx->selfid_size = stat & 0x1fff;
  859. handle_selfid(lynx, host);
  860. } else {
  861. quadlet_t *q_data = lynx->rcv_page;
  862. if ((*q_data >> 4 & 0xf) == TCODE_READQ_RESPONSE
  863. || (*q_data >> 4 & 0xf) == TCODE_WRITEQ) {
  864. cpu_to_be32s(q_data + 3);
  865. }
  866. hpsb_packet_received(host, q_data, stat & 0x1fff, 0);
  867. }
  868. run_pcl(lynx, lynx->rcv_pcl_start, CHANNEL_ASYNC_RCV);
  869. }
  870. return IRQ_HANDLED;
  871. }
  872. static void iso_rcv_bh(struct ti_lynx *lynx)
  873. {
  874. unsigned int idx;
  875. quadlet_t *data;
  876. unsigned long flags;
  877. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  878. while (lynx->iso_rcv.used) {
  879. idx = lynx->iso_rcv.last;
  880. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  881. data = lynx->iso_rcv.page[idx / ISORCV_PER_PAGE]
  882. + (idx % ISORCV_PER_PAGE) * MAX_ISORCV_SIZE;
  883. if ((*data >> 16) + 4 != (lynx->iso_rcv.stat[idx] & 0x1fff)) {
  884. PRINT(KERN_ERR, lynx->id,
  885. "iso length mismatch 0x%08x/0x%08x", *data,
  886. lynx->iso_rcv.stat[idx]);
  887. }
  888. if (lynx->iso_rcv.stat[idx]
  889. & (DMA_CHAN_STAT_PCIERR | DMA_CHAN_STAT_PKTERR)) {
  890. PRINT(KERN_INFO, lynx->id,
  891. "iso receive error on %d to 0x%p", idx, data);
  892. } else {
  893. hpsb_packet_received(lynx->host, data,
  894. lynx->iso_rcv.stat[idx] & 0x1fff,
  895. 0);
  896. }
  897. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  898. lynx->iso_rcv.last = (idx + 1) % NUM_ISORCV_PCL;
  899. lynx->iso_rcv.used--;
  900. }
  901. if (lynx->iso_rcv.chan_count) {
  902. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
  903. DMA_WORD1_CMP_ENABLE_MASTER);
  904. }
  905. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  906. }
  907. static void remove_card(struct pci_dev *dev)
  908. {
  909. struct ti_lynx *lynx;
  910. struct device *lynx_dev;
  911. int i;
  912. lynx = pci_get_drvdata(dev);
  913. if (!lynx) return;
  914. pci_set_drvdata(dev, NULL);
  915. lynx_dev = get_device(&lynx->host->device);
  916. switch (lynx->state) {
  917. case is_host:
  918. reg_write(lynx, PCI_INT_ENABLE, 0);
  919. hpsb_remove_host(lynx->host);
  920. case have_intr:
  921. reg_write(lynx, PCI_INT_ENABLE, 0);
  922. free_irq(lynx->dev->irq, lynx);
  923. /* Disable IRM Contender and LCtrl */
  924. if (lynx->phyic.reg_1394a)
  925. set_phy_reg(lynx, 4, ~0xc0 & get_phy_reg(lynx, 4));
  926. /* Let all other nodes know to ignore us */
  927. lynx_devctl(lynx->host, RESET_BUS, LONG_RESET_NO_FORCE_ROOT);
  928. case have_iomappings:
  929. reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
  930. /* Fix buggy cards with autoboot pin not tied low: */
  931. reg_write(lynx, DMA0_CHAN_CTRL, 0);
  932. iounmap(lynx->registers);
  933. iounmap(lynx->local_rom);
  934. iounmap(lynx->local_ram);
  935. iounmap(lynx->aux_port);
  936. case have_1394_buffers:
  937. for (i = 0; i < ISORCV_PAGES; i++) {
  938. if (lynx->iso_rcv.page[i]) {
  939. pci_free_consistent(lynx->dev, PAGE_SIZE,
  940. lynx->iso_rcv.page[i],
  941. lynx->iso_rcv.page_dma[i]);
  942. }
  943. }
  944. pci_free_consistent(lynx->dev, PAGE_SIZE, lynx->rcv_page,
  945. lynx->rcv_page_dma);
  946. case have_aux_buf:
  947. case have_pcl_mem:
  948. pci_free_consistent(lynx->dev, LOCALRAM_SIZE, lynx->pcl_mem,
  949. lynx->pcl_mem_dma);
  950. case clear:
  951. /* do nothing - already freed */
  952. ;
  953. }
  954. tasklet_kill(&lynx->iso_rcv.tq);
  955. if (lynx_dev)
  956. put_device(lynx_dev);
  957. }
  958. static int __devinit add_card(struct pci_dev *dev,
  959. const struct pci_device_id *devid_is_unused)
  960. {
  961. #define FAIL(fmt, args...) do { \
  962. PRINT_G(KERN_ERR, fmt , ## args); \
  963. remove_card(dev); \
  964. return error; \
  965. } while (0)
  966. char irq_buf[16];
  967. struct hpsb_host *host;
  968. struct ti_lynx *lynx; /* shortcut to currently handled device */
  969. struct ti_pcl pcl;
  970. u32 *pcli;
  971. int i;
  972. int error;
  973. error = -ENXIO;
  974. if (pci_set_dma_mask(dev, DMA_32BIT_MASK))
  975. FAIL("DMA address limits not supported for PCILynx hardware");
  976. if (pci_enable_device(dev))
  977. FAIL("failed to enable PCILynx hardware");
  978. pci_set_master(dev);
  979. error = -ENOMEM;
  980. host = hpsb_alloc_host(&lynx_driver, sizeof(struct ti_lynx), &dev->dev);
  981. if (!host) FAIL("failed to allocate control structure memory");
  982. lynx = host->hostdata;
  983. lynx->id = card_id++;
  984. lynx->dev = dev;
  985. lynx->state = clear;
  986. lynx->host = host;
  987. host->pdev = dev;
  988. pci_set_drvdata(dev, lynx);
  989. spin_lock_init(&lynx->lock);
  990. spin_lock_init(&lynx->phy_reg_lock);
  991. lynx->pcl_mem = pci_alloc_consistent(dev, LOCALRAM_SIZE,
  992. &lynx->pcl_mem_dma);
  993. if (lynx->pcl_mem != NULL) {
  994. lynx->state = have_pcl_mem;
  995. PRINT(KERN_INFO, lynx->id,
  996. "allocated PCL memory %d Bytes @ 0x%p", LOCALRAM_SIZE,
  997. lynx->pcl_mem);
  998. } else {
  999. FAIL("failed to allocate PCL memory area");
  1000. }
  1001. lynx->rcv_page = pci_alloc_consistent(dev, PAGE_SIZE,
  1002. &lynx->rcv_page_dma);
  1003. if (lynx->rcv_page == NULL) {
  1004. FAIL("failed to allocate receive buffer");
  1005. }
  1006. lynx->state = have_1394_buffers;
  1007. for (i = 0; i < ISORCV_PAGES; i++) {
  1008. lynx->iso_rcv.page[i] =
  1009. pci_alloc_consistent(dev, PAGE_SIZE,
  1010. &lynx->iso_rcv.page_dma[i]);
  1011. if (lynx->iso_rcv.page[i] == NULL) {
  1012. FAIL("failed to allocate iso receive buffers");
  1013. }
  1014. }
  1015. lynx->registers = ioremap_nocache(pci_resource_start(dev,0),
  1016. PCILYNX_MAX_REGISTER);
  1017. lynx->local_ram = ioremap(pci_resource_start(dev,1), PCILYNX_MAX_MEMORY);
  1018. lynx->aux_port = ioremap(pci_resource_start(dev,2), PCILYNX_MAX_MEMORY);
  1019. lynx->local_rom = ioremap(pci_resource_start(dev,PCI_ROM_RESOURCE),
  1020. PCILYNX_MAX_MEMORY);
  1021. lynx->state = have_iomappings;
  1022. if (lynx->registers == NULL) {
  1023. FAIL("failed to remap registers - card not accessible");
  1024. }
  1025. reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
  1026. /* Fix buggy cards with autoboot pin not tied low: */
  1027. reg_write(lynx, DMA0_CHAN_CTRL, 0);
  1028. sprintf (irq_buf, "%d", dev->irq);
  1029. if (!request_irq(dev->irq, lynx_irq_handler, IRQF_SHARED,
  1030. PCILYNX_DRIVER_NAME, lynx)) {
  1031. PRINT(KERN_INFO, lynx->id, "allocated interrupt %s", irq_buf);
  1032. lynx->state = have_intr;
  1033. } else {
  1034. FAIL("failed to allocate shared interrupt %s", irq_buf);
  1035. }
  1036. /* alloc_pcl return values are not checked, it is expected that the
  1037. * provided PCL space is sufficient for the initial allocations */
  1038. lynx->rcv_pcl = alloc_pcl(lynx);
  1039. lynx->rcv_pcl_start = alloc_pcl(lynx);
  1040. lynx->async.pcl = alloc_pcl(lynx);
  1041. lynx->async.pcl_start = alloc_pcl(lynx);
  1042. lynx->iso_send.pcl = alloc_pcl(lynx);
  1043. lynx->iso_send.pcl_start = alloc_pcl(lynx);
  1044. for (i = 0; i < NUM_ISORCV_PCL; i++) {
  1045. lynx->iso_rcv.pcl[i] = alloc_pcl(lynx);
  1046. }
  1047. lynx->iso_rcv.pcl_start = alloc_pcl(lynx);
  1048. /* all allocations successful - simple init stuff follows */
  1049. reg_write(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
  1050. tasklet_init(&lynx->iso_rcv.tq, (void (*)(unsigned long))iso_rcv_bh,
  1051. (unsigned long)lynx);
  1052. spin_lock_init(&lynx->iso_rcv.lock);
  1053. spin_lock_init(&lynx->async.queue_lock);
  1054. lynx->async.channel = CHANNEL_ASYNC_SEND;
  1055. spin_lock_init(&lynx->iso_send.queue_lock);
  1056. lynx->iso_send.channel = CHANNEL_ISO_SEND;
  1057. PRINT(KERN_INFO, lynx->id, "remapped memory spaces reg 0x%p, rom 0x%p, "
  1058. "ram 0x%p, aux 0x%p", lynx->registers, lynx->local_rom,
  1059. lynx->local_ram, lynx->aux_port);
  1060. /* now, looking for PHY register set */
  1061. if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
  1062. lynx->phyic.reg_1394a = 1;
  1063. PRINT(KERN_INFO, lynx->id,
  1064. "found 1394a conform PHY (using extended register set)");
  1065. lynx->phyic.vendor = get_phy_vendorid(lynx);
  1066. lynx->phyic.product = get_phy_productid(lynx);
  1067. } else {
  1068. lynx->phyic.reg_1394a = 0;
  1069. PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
  1070. }
  1071. lynx->selfid_size = -1;
  1072. lynx->phy_reg0 = -1;
  1073. INIT_LIST_HEAD(&lynx->async.queue);
  1074. INIT_LIST_HEAD(&lynx->async.pcl_queue);
  1075. INIT_LIST_HEAD(&lynx->iso_send.queue);
  1076. INIT_LIST_HEAD(&lynx->iso_send.pcl_queue);
  1077. pcl.next = pcl_bus(lynx, lynx->rcv_pcl);
  1078. put_pcl(lynx, lynx->rcv_pcl_start, &pcl);
  1079. pcl.next = PCL_NEXT_INVALID;
  1080. pcl.async_error_next = PCL_NEXT_INVALID;
  1081. pcl.buffer[0].control = PCL_CMD_RCV | 16;
  1082. #ifndef __BIG_ENDIAN
  1083. pcl.buffer[0].control |= PCL_BIGENDIAN;
  1084. #endif
  1085. pcl.buffer[1].control = PCL_LAST_BUFF | 4080;
  1086. pcl.buffer[0].pointer = lynx->rcv_page_dma;
  1087. pcl.buffer[1].pointer = lynx->rcv_page_dma + 16;
  1088. put_pcl(lynx, lynx->rcv_pcl, &pcl);
  1089. pcl.next = pcl_bus(lynx, lynx->async.pcl);
  1090. pcl.async_error_next = pcl_bus(lynx, lynx->async.pcl);
  1091. put_pcl(lynx, lynx->async.pcl_start, &pcl);
  1092. pcl.next = pcl_bus(lynx, lynx->iso_send.pcl);
  1093. pcl.async_error_next = PCL_NEXT_INVALID;
  1094. put_pcl(lynx, lynx->iso_send.pcl_start, &pcl);
  1095. pcl.next = PCL_NEXT_INVALID;
  1096. pcl.async_error_next = PCL_NEXT_INVALID;
  1097. pcl.buffer[0].control = PCL_CMD_RCV | 4;
  1098. #ifndef __BIG_ENDIAN
  1099. pcl.buffer[0].control |= PCL_BIGENDIAN;
  1100. #endif
  1101. pcl.buffer[1].control = PCL_LAST_BUFF | 2044;
  1102. for (i = 0; i < NUM_ISORCV_PCL; i++) {
  1103. int page = i / ISORCV_PER_PAGE;
  1104. int sec = i % ISORCV_PER_PAGE;
  1105. pcl.buffer[0].pointer = lynx->iso_rcv.page_dma[page]
  1106. + sec * MAX_ISORCV_SIZE;
  1107. pcl.buffer[1].pointer = pcl.buffer[0].pointer + 4;
  1108. put_pcl(lynx, lynx->iso_rcv.pcl[i], &pcl);
  1109. }
  1110. pcli = (u32 *)&pcl;
  1111. for (i = 0; i < NUM_ISORCV_PCL; i++) {
  1112. pcli[i] = pcl_bus(lynx, lynx->iso_rcv.pcl[i]);
  1113. }
  1114. put_pcl(lynx, lynx->iso_rcv.pcl_start, &pcl);
  1115. /* FIFO sizes from left to right: ITF=48 ATF=48 GRF=160 */
  1116. reg_write(lynx, FIFO_SIZES, 0x003030a0);
  1117. /* 20 byte threshold before triggering PCI transfer */
  1118. reg_write(lynx, DMA_GLOBAL_REGISTER, 0x2<<24);
  1119. /* threshold on both send FIFOs before transmitting:
  1120. FIFO size - cache line size - 1 */
  1121. i = reg_read(lynx, PCI_LATENCY_CACHELINE) & 0xff;
  1122. i = 0x30 - i - 1;
  1123. reg_write(lynx, FIFO_XMIT_THRESHOLD, (i << 8) | i);
  1124. reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_1394);
  1125. reg_write(lynx, LINK_INT_ENABLE, LINK_INT_PHY_TIMEOUT
  1126. | LINK_INT_PHY_REG_RCVD | LINK_INT_PHY_BUSRESET
  1127. | LINK_INT_ISO_STUCK | LINK_INT_ASYNC_STUCK
  1128. | LINK_INT_SENT_REJECT | LINK_INT_TX_INVALID_TC
  1129. | LINK_INT_GRF_OVERFLOW | LINK_INT_ITF_UNDERFLOW
  1130. | LINK_INT_ATF_UNDERFLOW);
  1131. reg_write(lynx, DMA_WORD0_CMP_VALUE(CHANNEL_ASYNC_RCV), 0);
  1132. reg_write(lynx, DMA_WORD0_CMP_ENABLE(CHANNEL_ASYNC_RCV), 0xa<<4);
  1133. reg_write(lynx, DMA_WORD1_CMP_VALUE(CHANNEL_ASYNC_RCV), 0);
  1134. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ASYNC_RCV),
  1135. DMA_WORD1_CMP_MATCH_LOCAL_NODE | DMA_WORD1_CMP_MATCH_BROADCAST
  1136. | DMA_WORD1_CMP_MATCH_EXACT | DMA_WORD1_CMP_MATCH_BUS_BCAST
  1137. | DMA_WORD1_CMP_ENABLE_SELF_ID | DMA_WORD1_CMP_ENABLE_MASTER);
  1138. run_pcl(lynx, lynx->rcv_pcl_start, CHANNEL_ASYNC_RCV);
  1139. reg_write(lynx, DMA_WORD0_CMP_VALUE(CHANNEL_ISO_RCV), 0);
  1140. reg_write(lynx, DMA_WORD0_CMP_ENABLE(CHANNEL_ISO_RCV), 0x9<<4);
  1141. reg_write(lynx, DMA_WORD1_CMP_VALUE(CHANNEL_ISO_RCV), 0);
  1142. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV), 0);
  1143. run_sub_pcl(lynx, lynx->iso_rcv.pcl_start, 0, CHANNEL_ISO_RCV);
  1144. reg_write(lynx, LINK_CONTROL, LINK_CONTROL_RCV_CMP_VALID
  1145. | LINK_CONTROL_TX_ISO_EN | LINK_CONTROL_RX_ISO_EN
  1146. | LINK_CONTROL_TX_ASYNC_EN | LINK_CONTROL_RX_ASYNC_EN
  1147. | LINK_CONTROL_RESET_TX | LINK_CONTROL_RESET_RX);
  1148. if (!lynx->phyic.reg_1394a) {
  1149. if (!hpsb_disable_irm) {
  1150. /* attempt to enable contender bit -FIXME- would this
  1151. * work elsewhere? */
  1152. reg_set_bits(lynx, GPIO_CTRL_A, 0x1);
  1153. reg_write(lynx, GPIO_DATA_BASE + 0x3c, 0x1);
  1154. }
  1155. } else {
  1156. /* set the contender (if appropriate) and LCtrl bit in the
  1157. * extended PHY register set. (Should check that PHY_02_EXTENDED
  1158. * is set in register 2?)
  1159. */
  1160. i = get_phy_reg(lynx, 4);
  1161. i |= PHY_04_LCTRL;
  1162. if (hpsb_disable_irm)
  1163. i &= ~PHY_04_CONTENDER;
  1164. else
  1165. i |= PHY_04_CONTENDER;
  1166. if (i != -1) set_phy_reg(lynx, 4, i);
  1167. }
  1168. if (!skip_eeprom)
  1169. {
  1170. /* needed for i2c communication with serial eeprom */
  1171. struct i2c_adapter *i2c_ad;
  1172. struct i2c_algo_bit_data i2c_adapter_data;
  1173. error = -ENOMEM;
  1174. i2c_ad = kmalloc(sizeof(*i2c_ad), SLAB_KERNEL);
  1175. if (!i2c_ad) FAIL("failed to allocate I2C adapter memory");
  1176. memcpy(i2c_ad, &bit_ops, sizeof(struct i2c_adapter));
  1177. i2c_adapter_data = bit_data;
  1178. i2c_ad->algo_data = &i2c_adapter_data;
  1179. i2c_adapter_data.data = lynx;
  1180. PRINTD(KERN_DEBUG, lynx->id,"original eeprom control: %d",
  1181. reg_read(lynx, SERIAL_EEPROM_CONTROL));
  1182. /* reset hardware to sane state */
  1183. lynx->i2c_driven_state = 0x00000070;
  1184. reg_write(lynx, SERIAL_EEPROM_CONTROL, lynx->i2c_driven_state);
  1185. if (i2c_bit_add_bus(i2c_ad) < 0)
  1186. {
  1187. kfree(i2c_ad);
  1188. error = -ENXIO;
  1189. FAIL("unable to register i2c");
  1190. }
  1191. else
  1192. {
  1193. /* do i2c stuff */
  1194. unsigned char i2c_cmd = 0x10;
  1195. struct i2c_msg msg[2] = { { 0x50, 0, 1, &i2c_cmd },
  1196. { 0x50, I2C_M_RD, 20, (unsigned char*) lynx->bus_info_block }
  1197. };
  1198. /* we use i2c_transfer, because i2c_smbus_read_block_data does not work properly and we
  1199. do it more efficiently in one transaction rather then using several reads */
  1200. if (i2c_transfer(i2c_ad, msg, 2) < 0) {
  1201. PRINT(KERN_ERR, lynx->id, "unable to read bus info block from i2c");
  1202. } else {
  1203. int i;
  1204. PRINT(KERN_INFO, lynx->id, "got bus info block from serial eeprom");
  1205. /* FIXME: probably we shoud rewrite the max_rec, max_ROM(1394a),
  1206. * generation(1394a) and link_spd(1394a) field and recalculate
  1207. * the CRC */
  1208. for (i = 0; i < 5 ; i++)
  1209. PRINTD(KERN_DEBUG, lynx->id, "Businfo block quadlet %i: %08x",
  1210. i, be32_to_cpu(lynx->bus_info_block[i]));
  1211. /* info_length, crc_length and 1394 magic number to check, if it is really a bus info block */
  1212. if (((be32_to_cpu(lynx->bus_info_block[0]) & 0xffff0000) == 0x04040000) &&
  1213. (lynx->bus_info_block[1] == __constant_cpu_to_be32(0x31333934)))
  1214. {
  1215. PRINT(KERN_DEBUG, lynx->id, "read a valid bus info block from");
  1216. } else {
  1217. kfree(i2c_ad);
  1218. error = -ENXIO;
  1219. FAIL("read something from serial eeprom, but it does not seem to be a valid bus info block");
  1220. }
  1221. }
  1222. i2c_bit_del_bus(i2c_ad);
  1223. kfree(i2c_ad);
  1224. }
  1225. }
  1226. host->csr.guid_hi = be32_to_cpu(lynx->bus_info_block[3]);
  1227. host->csr.guid_lo = be32_to_cpu(lynx->bus_info_block[4]);
  1228. host->csr.cyc_clk_acc = (be32_to_cpu(lynx->bus_info_block[2]) >> 16) & 0xff;
  1229. host->csr.max_rec = (be32_to_cpu(lynx->bus_info_block[2]) >> 12) & 0xf;
  1230. if (!lynx->phyic.reg_1394a)
  1231. host->csr.lnk_spd = (get_phy_reg(lynx, 2) & 0xc0) >> 6;
  1232. else
  1233. host->csr.lnk_spd = be32_to_cpu(lynx->bus_info_block[2]) & 0x7;
  1234. if (hpsb_add_host(host)) {
  1235. error = -ENOMEM;
  1236. FAIL("Failed to register host with highlevel");
  1237. }
  1238. lynx->state = is_host;
  1239. return 0;
  1240. #undef FAIL
  1241. }
  1242. static struct pci_device_id pci_table[] = {
  1243. {
  1244. .vendor = PCI_VENDOR_ID_TI,
  1245. .device = PCI_DEVICE_ID_TI_PCILYNX,
  1246. .subvendor = PCI_ANY_ID,
  1247. .subdevice = PCI_ANY_ID,
  1248. },
  1249. { } /* Terminating entry */
  1250. };
  1251. static struct pci_driver lynx_pci_driver = {
  1252. .name = PCILYNX_DRIVER_NAME,
  1253. .id_table = pci_table,
  1254. .probe = add_card,
  1255. .remove = remove_card,
  1256. };
  1257. static struct hpsb_host_driver lynx_driver = {
  1258. .owner = THIS_MODULE,
  1259. .name = PCILYNX_DRIVER_NAME,
  1260. .set_hw_config_rom = NULL,
  1261. .transmit_packet = lynx_transmit,
  1262. .devctl = lynx_devctl,
  1263. .isoctl = NULL,
  1264. };
  1265. MODULE_AUTHOR("Andreas E. Bombe <andreas.bombe@munich.netsurf.de>");
  1266. MODULE_DESCRIPTION("driver for Texas Instruments PCI Lynx IEEE-1394 controller");
  1267. MODULE_LICENSE("GPL");
  1268. MODULE_SUPPORTED_DEVICE("pcilynx");
  1269. MODULE_DEVICE_TABLE(pci, pci_table);
  1270. static int __init pcilynx_init(void)
  1271. {
  1272. int ret;
  1273. ret = pci_register_driver(&lynx_pci_driver);
  1274. if (ret < 0) {
  1275. PRINT_G(KERN_ERR, "PCI module init failed");
  1276. return ret;
  1277. }
  1278. return 0;
  1279. }
  1280. static void __exit pcilynx_cleanup(void)
  1281. {
  1282. pci_unregister_driver(&lynx_pci_driver);
  1283. }
  1284. module_init(pcilynx_init);
  1285. module_exit(pcilynx_cleanup);