pcilynx.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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 = kmemdup(&bit_ops, sizeof(*i2c_ad), GFP_KERNEL);
  1175. if (!i2c_ad) FAIL("failed to allocate I2C adapter memory");
  1176. i2c_adapter_data = bit_data;
  1177. i2c_ad->algo_data = &i2c_adapter_data;
  1178. i2c_adapter_data.data = lynx;
  1179. PRINTD(KERN_DEBUG, lynx->id,"original eeprom control: %d",
  1180. reg_read(lynx, SERIAL_EEPROM_CONTROL));
  1181. /* reset hardware to sane state */
  1182. lynx->i2c_driven_state = 0x00000070;
  1183. reg_write(lynx, SERIAL_EEPROM_CONTROL, lynx->i2c_driven_state);
  1184. if (i2c_bit_add_bus(i2c_ad) < 0)
  1185. {
  1186. kfree(i2c_ad);
  1187. error = -ENXIO;
  1188. FAIL("unable to register i2c");
  1189. }
  1190. else
  1191. {
  1192. /* do i2c stuff */
  1193. unsigned char i2c_cmd = 0x10;
  1194. struct i2c_msg msg[2] = { { 0x50, 0, 1, &i2c_cmd },
  1195. { 0x50, I2C_M_RD, 20, (unsigned char*) lynx->bus_info_block }
  1196. };
  1197. /* we use i2c_transfer, because i2c_smbus_read_block_data does not work properly and we
  1198. do it more efficiently in one transaction rather then using several reads */
  1199. if (i2c_transfer(i2c_ad, msg, 2) < 0) {
  1200. PRINT(KERN_ERR, lynx->id, "unable to read bus info block from i2c");
  1201. } else {
  1202. int i;
  1203. PRINT(KERN_INFO, lynx->id, "got bus info block from serial eeprom");
  1204. /* FIXME: probably we shoud rewrite the max_rec, max_ROM(1394a),
  1205. * generation(1394a) and link_spd(1394a) field and recalculate
  1206. * the CRC */
  1207. for (i = 0; i < 5 ; i++)
  1208. PRINTD(KERN_DEBUG, lynx->id, "Businfo block quadlet %i: %08x",
  1209. i, be32_to_cpu(lynx->bus_info_block[i]));
  1210. /* info_length, crc_length and 1394 magic number to check, if it is really a bus info block */
  1211. if (((be32_to_cpu(lynx->bus_info_block[0]) & 0xffff0000) == 0x04040000) &&
  1212. (lynx->bus_info_block[1] == __constant_cpu_to_be32(0x31333934)))
  1213. {
  1214. PRINT(KERN_DEBUG, lynx->id, "read a valid bus info block from");
  1215. } else {
  1216. kfree(i2c_ad);
  1217. error = -ENXIO;
  1218. FAIL("read something from serial eeprom, but it does not seem to be a valid bus info block");
  1219. }
  1220. }
  1221. i2c_del_adapter(i2c_ad);
  1222. kfree(i2c_ad);
  1223. }
  1224. }
  1225. host->csr.guid_hi = be32_to_cpu(lynx->bus_info_block[3]);
  1226. host->csr.guid_lo = be32_to_cpu(lynx->bus_info_block[4]);
  1227. host->csr.cyc_clk_acc = (be32_to_cpu(lynx->bus_info_block[2]) >> 16) & 0xff;
  1228. host->csr.max_rec = (be32_to_cpu(lynx->bus_info_block[2]) >> 12) & 0xf;
  1229. if (!lynx->phyic.reg_1394a)
  1230. host->csr.lnk_spd = (get_phy_reg(lynx, 2) & 0xc0) >> 6;
  1231. else
  1232. host->csr.lnk_spd = be32_to_cpu(lynx->bus_info_block[2]) & 0x7;
  1233. if (hpsb_add_host(host)) {
  1234. error = -ENOMEM;
  1235. FAIL("Failed to register host with highlevel");
  1236. }
  1237. lynx->state = is_host;
  1238. return 0;
  1239. #undef FAIL
  1240. }
  1241. static struct pci_device_id pci_table[] = {
  1242. {
  1243. .vendor = PCI_VENDOR_ID_TI,
  1244. .device = PCI_DEVICE_ID_TI_PCILYNX,
  1245. .subvendor = PCI_ANY_ID,
  1246. .subdevice = PCI_ANY_ID,
  1247. },
  1248. { } /* Terminating entry */
  1249. };
  1250. static struct pci_driver lynx_pci_driver = {
  1251. .name = PCILYNX_DRIVER_NAME,
  1252. .id_table = pci_table,
  1253. .probe = add_card,
  1254. .remove = remove_card,
  1255. };
  1256. static struct hpsb_host_driver lynx_driver = {
  1257. .owner = THIS_MODULE,
  1258. .name = PCILYNX_DRIVER_NAME,
  1259. .set_hw_config_rom = NULL,
  1260. .transmit_packet = lynx_transmit,
  1261. .devctl = lynx_devctl,
  1262. .isoctl = NULL,
  1263. };
  1264. MODULE_AUTHOR("Andreas E. Bombe <andreas.bombe@munich.netsurf.de>");
  1265. MODULE_DESCRIPTION("driver for Texas Instruments PCI Lynx IEEE-1394 controller");
  1266. MODULE_LICENSE("GPL");
  1267. MODULE_SUPPORTED_DEVICE("pcilynx");
  1268. MODULE_DEVICE_TABLE(pci, pci_table);
  1269. static int __init pcilynx_init(void)
  1270. {
  1271. int ret;
  1272. ret = pci_register_driver(&lynx_pci_driver);
  1273. if (ret < 0) {
  1274. PRINT_G(KERN_ERR, "PCI module init failed");
  1275. return ret;
  1276. }
  1277. return 0;
  1278. }
  1279. static void __exit pcilynx_cleanup(void)
  1280. {
  1281. pci_unregister_driver(&lynx_pci_driver);
  1282. }
  1283. module_init(pcilynx_init);
  1284. module_exit(pcilynx_cleanup);