pcilynx.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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. #if 0 /* has been removed from ieee1394 core */
  392. d = (what == hpsb_iso ? &lynx->iso_send : &lynx->async);
  393. #else
  394. d = &lynx->async;
  395. #endif
  396. if (!list_empty(&d->pcl_queue)) {
  397. PRINT(KERN_ERR, lynx->id, "trying to queue a new packet in nonempty fifo");
  398. BUG();
  399. }
  400. packet = driver_packet(d->queue.next);
  401. list_move_tail(&packet->driver_list, &d->pcl_queue);
  402. d->header_dma = pci_map_single(lynx->dev, packet->header,
  403. packet->header_size, PCI_DMA_TODEVICE);
  404. if (packet->data_size) {
  405. d->data_dma = pci_map_single(lynx->dev, packet->data,
  406. packet->data_size,
  407. PCI_DMA_TODEVICE);
  408. } else {
  409. d->data_dma = 0;
  410. }
  411. pcl.next = PCL_NEXT_INVALID;
  412. pcl.async_error_next = PCL_NEXT_INVALID;
  413. pcl.pcl_status = 0;
  414. pcl.buffer[0].control = packet->speed_code << 14 | packet->header_size;
  415. #ifndef __BIG_ENDIAN
  416. pcl.buffer[0].control |= PCL_BIGENDIAN;
  417. #endif
  418. pcl.buffer[0].pointer = d->header_dma;
  419. pcl.buffer[1].control = PCL_LAST_BUFF | packet->data_size;
  420. pcl.buffer[1].pointer = d->data_dma;
  421. switch (packet->type) {
  422. case hpsb_async:
  423. pcl.buffer[0].control |= PCL_CMD_XMT;
  424. break;
  425. #if 0 /* has been removed from ieee1394 core */
  426. case hpsb_iso:
  427. pcl.buffer[0].control |= PCL_CMD_XMT | PCL_ISOMODE;
  428. break;
  429. #endif
  430. case hpsb_raw:
  431. pcl.buffer[0].control |= PCL_CMD_UNFXMT;
  432. break;
  433. }
  434. put_pcl(lynx, d->pcl, &pcl);
  435. run_pcl(lynx, d->pcl_start, d->channel);
  436. }
  437. /* called from subsystem core */
  438. static int lynx_transmit(struct hpsb_host *host, struct hpsb_packet *packet)
  439. {
  440. struct ti_lynx *lynx = host->hostdata;
  441. struct lynx_send_data *d;
  442. unsigned long flags;
  443. if (packet->data_size >= 4096) {
  444. PRINT(KERN_ERR, lynx->id, "transmit packet data too big (%Zd)",
  445. packet->data_size);
  446. return -EOVERFLOW;
  447. }
  448. switch (packet->type) {
  449. case hpsb_async:
  450. case hpsb_raw:
  451. d = &lynx->async;
  452. break;
  453. #if 0 /* has been removed from ieee1394 core */
  454. case hpsb_iso:
  455. d = &lynx->iso_send;
  456. break;
  457. #endif
  458. default:
  459. PRINT(KERN_ERR, lynx->id, "invalid packet type %d",
  460. packet->type);
  461. return -EINVAL;
  462. }
  463. if (packet->tcode == TCODE_WRITEQ
  464. || packet->tcode == TCODE_READQ_RESPONSE) {
  465. cpu_to_be32s(&packet->header[3]);
  466. }
  467. spin_lock_irqsave(&d->queue_lock, flags);
  468. list_add_tail(&packet->driver_list, &d->queue);
  469. if (list_empty(&d->pcl_queue))
  470. send_next(lynx, packet->type);
  471. spin_unlock_irqrestore(&d->queue_lock, flags);
  472. return 0;
  473. }
  474. /* called from subsystem core */
  475. static int lynx_devctl(struct hpsb_host *host, enum devctl_cmd cmd, int arg)
  476. {
  477. struct ti_lynx *lynx = host->hostdata;
  478. int retval = 0;
  479. struct hpsb_packet *packet;
  480. LIST_HEAD(packet_list);
  481. unsigned long flags;
  482. int phy_reg;
  483. switch (cmd) {
  484. case RESET_BUS:
  485. if (reg_read(lynx, LINK_INT_STATUS) & LINK_INT_PHY_BUSRESET) {
  486. retval = 0;
  487. break;
  488. }
  489. switch (arg) {
  490. case SHORT_RESET:
  491. if (lynx->phyic.reg_1394a) {
  492. phy_reg = get_phy_reg(lynx, 5);
  493. if (phy_reg == -1) {
  494. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  495. retval = -1;
  496. break;
  497. }
  498. phy_reg |= 0x40;
  499. PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset) on request");
  500. lynx->selfid_size = -1;
  501. lynx->phy_reg0 = -1;
  502. set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
  503. break;
  504. } else {
  505. PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
  506. /* fall through to long bus reset */
  507. }
  508. case LONG_RESET:
  509. phy_reg = get_phy_reg(lynx, 1);
  510. if (phy_reg == -1) {
  511. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  512. retval = -1;
  513. break;
  514. }
  515. phy_reg |= 0x40;
  516. PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset) on request");
  517. lynx->selfid_size = -1;
  518. lynx->phy_reg0 = -1;
  519. set_phy_reg(lynx, 1, phy_reg); /* clear RHB, set IBR */
  520. break;
  521. case SHORT_RESET_NO_FORCE_ROOT:
  522. if (lynx->phyic.reg_1394a) {
  523. phy_reg = get_phy_reg(lynx, 1);
  524. if (phy_reg == -1) {
  525. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  526. retval = -1;
  527. break;
  528. }
  529. if (phy_reg & 0x80) {
  530. phy_reg &= ~0x80;
  531. set_phy_reg(lynx, 1, phy_reg); /* clear RHB */
  532. }
  533. phy_reg = get_phy_reg(lynx, 5);
  534. if (phy_reg == -1) {
  535. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  536. retval = -1;
  537. break;
  538. }
  539. phy_reg |= 0x40;
  540. PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset, no force_root) on request");
  541. lynx->selfid_size = -1;
  542. lynx->phy_reg0 = -1;
  543. set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
  544. break;
  545. } else {
  546. PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
  547. /* fall through to long bus reset */
  548. }
  549. case LONG_RESET_NO_FORCE_ROOT:
  550. phy_reg = get_phy_reg(lynx, 1);
  551. if (phy_reg == -1) {
  552. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  553. retval = -1;
  554. break;
  555. }
  556. phy_reg &= ~0x80;
  557. phy_reg |= 0x40;
  558. PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset, no force_root) on request");
  559. lynx->selfid_size = -1;
  560. lynx->phy_reg0 = -1;
  561. set_phy_reg(lynx, 1, phy_reg); /* clear RHB, set IBR */
  562. break;
  563. case SHORT_RESET_FORCE_ROOT:
  564. if (lynx->phyic.reg_1394a) {
  565. phy_reg = get_phy_reg(lynx, 1);
  566. if (phy_reg == -1) {
  567. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  568. retval = -1;
  569. break;
  570. }
  571. if (!(phy_reg & 0x80)) {
  572. phy_reg |= 0x80;
  573. set_phy_reg(lynx, 1, phy_reg); /* set RHB */
  574. }
  575. phy_reg = get_phy_reg(lynx, 5);
  576. if (phy_reg == -1) {
  577. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  578. retval = -1;
  579. break;
  580. }
  581. phy_reg |= 0x40;
  582. PRINT(KERN_INFO, lynx->id, "resetting bus (short bus reset, force_root set) on request");
  583. lynx->selfid_size = -1;
  584. lynx->phy_reg0 = -1;
  585. set_phy_reg(lynx, 5, phy_reg); /* set ISBR */
  586. break;
  587. } else {
  588. PRINT(KERN_INFO, lynx->id, "cannot do short bus reset, because of old phy");
  589. /* fall through to long bus reset */
  590. }
  591. case LONG_RESET_FORCE_ROOT:
  592. phy_reg = get_phy_reg(lynx, 1);
  593. if (phy_reg == -1) {
  594. PRINT(KERN_ERR, lynx->id, "cannot reset bus, because read phy reg failed");
  595. retval = -1;
  596. break;
  597. }
  598. phy_reg |= 0xc0;
  599. PRINT(KERN_INFO, lynx->id, "resetting bus (long bus reset, force_root set) on request");
  600. lynx->selfid_size = -1;
  601. lynx->phy_reg0 = -1;
  602. set_phy_reg(lynx, 1, phy_reg); /* set IBR and RHB */
  603. break;
  604. default:
  605. PRINT(KERN_ERR, lynx->id, "unknown argument for reset_bus command %d", arg);
  606. retval = -1;
  607. }
  608. break;
  609. case GET_CYCLE_COUNTER:
  610. retval = reg_read(lynx, CYCLE_TIMER);
  611. break;
  612. case SET_CYCLE_COUNTER:
  613. reg_write(lynx, CYCLE_TIMER, arg);
  614. break;
  615. case SET_BUS_ID:
  616. reg_write(lynx, LINK_ID,
  617. (arg << 22) | (reg_read(lynx, LINK_ID) & 0x003f0000));
  618. break;
  619. case ACT_CYCLE_MASTER:
  620. if (arg) {
  621. reg_set_bits(lynx, LINK_CONTROL,
  622. LINK_CONTROL_CYCMASTER);
  623. } else {
  624. reg_clear_bits(lynx, LINK_CONTROL,
  625. LINK_CONTROL_CYCMASTER);
  626. }
  627. break;
  628. case CANCEL_REQUESTS:
  629. spin_lock_irqsave(&lynx->async.queue_lock, flags);
  630. reg_write(lynx, DMA_CHAN_CTRL(CHANNEL_ASYNC_SEND), 0);
  631. list_splice(&lynx->async.queue, &packet_list);
  632. INIT_LIST_HEAD(&lynx->async.queue);
  633. if (list_empty(&lynx->async.pcl_queue)) {
  634. spin_unlock_irqrestore(&lynx->async.queue_lock, flags);
  635. PRINTD(KERN_DEBUG, lynx->id, "no async packet in PCL to cancel");
  636. } else {
  637. struct ti_pcl pcl;
  638. u32 ack;
  639. struct hpsb_packet *packet;
  640. PRINT(KERN_INFO, lynx->id, "cancelling async packet, that was already in PCL");
  641. get_pcl(lynx, lynx->async.pcl, &pcl);
  642. packet = driver_packet(lynx->async.pcl_queue.next);
  643. list_del_init(&packet->driver_list);
  644. pci_unmap_single(lynx->dev, lynx->async.header_dma,
  645. packet->header_size, PCI_DMA_TODEVICE);
  646. if (packet->data_size) {
  647. pci_unmap_single(lynx->dev, lynx->async.data_dma,
  648. packet->data_size, PCI_DMA_TODEVICE);
  649. }
  650. spin_unlock_irqrestore(&lynx->async.queue_lock, flags);
  651. if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
  652. if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
  653. ack = (pcl.pcl_status >> 15) & 0xf;
  654. PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
  655. ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
  656. } else {
  657. ack = (pcl.pcl_status >> 15) & 0xf;
  658. }
  659. } else {
  660. PRINT(KERN_INFO, lynx->id, "async packet was not completed");
  661. ack = ACKX_ABORTED;
  662. }
  663. hpsb_packet_sent(host, packet, ack);
  664. }
  665. while (!list_empty(&packet_list)) {
  666. packet = driver_packet(packet_list.next);
  667. list_del_init(&packet->driver_list);
  668. hpsb_packet_sent(host, packet, ACKX_ABORTED);
  669. }
  670. break;
  671. #if 0 /* has been removed from ieee1394 core */
  672. case ISO_LISTEN_CHANNEL:
  673. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  674. if (lynx->iso_rcv.chan_count++ == 0) {
  675. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
  676. DMA_WORD1_CMP_ENABLE_MASTER);
  677. }
  678. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  679. break;
  680. case ISO_UNLISTEN_CHANNEL:
  681. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  682. if (--lynx->iso_rcv.chan_count == 0) {
  683. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
  684. 0);
  685. }
  686. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  687. break;
  688. #endif
  689. default:
  690. PRINT(KERN_ERR, lynx->id, "unknown devctl command %d", cmd);
  691. retval = -1;
  692. }
  693. return retval;
  694. }
  695. /***************************************
  696. * IEEE-1394 functionality section END *
  697. ***************************************/
  698. /********************************************************
  699. * Global stuff (interrupt handler, init/shutdown code) *
  700. ********************************************************/
  701. static irqreturn_t lynx_irq_handler(int irq, void *dev_id)
  702. {
  703. struct ti_lynx *lynx = (struct ti_lynx *)dev_id;
  704. struct hpsb_host *host = lynx->host;
  705. u32 intmask;
  706. u32 linkint;
  707. linkint = reg_read(lynx, LINK_INT_STATUS);
  708. intmask = reg_read(lynx, PCI_INT_STATUS);
  709. if (!(intmask & PCI_INT_INT_PEND))
  710. return IRQ_NONE;
  711. PRINTD(KERN_DEBUG, lynx->id, "interrupt: 0x%08x / 0x%08x", intmask,
  712. linkint);
  713. reg_write(lynx, LINK_INT_STATUS, linkint);
  714. reg_write(lynx, PCI_INT_STATUS, intmask);
  715. if (intmask & PCI_INT_1394) {
  716. if (linkint & LINK_INT_PHY_TIMEOUT) {
  717. PRINT(KERN_INFO, lynx->id, "PHY timeout occurred");
  718. }
  719. if (linkint & LINK_INT_PHY_BUSRESET) {
  720. PRINT(KERN_INFO, lynx->id, "bus reset interrupt");
  721. lynx->selfid_size = -1;
  722. lynx->phy_reg0 = -1;
  723. if (!host->in_bus_reset)
  724. hpsb_bus_reset(host);
  725. }
  726. if (linkint & LINK_INT_PHY_REG_RCVD) {
  727. u32 reg;
  728. spin_lock(&lynx->phy_reg_lock);
  729. reg = reg_read(lynx, LINK_PHY);
  730. spin_unlock(&lynx->phy_reg_lock);
  731. if (!host->in_bus_reset) {
  732. PRINT(KERN_INFO, lynx->id,
  733. "phy reg received without reset");
  734. } else if (reg & 0xf00) {
  735. PRINT(KERN_INFO, lynx->id,
  736. "unsolicited phy reg %d received",
  737. (reg >> 8) & 0xf);
  738. } else {
  739. lynx->phy_reg0 = reg & 0xff;
  740. handle_selfid(lynx, host);
  741. }
  742. }
  743. if (linkint & LINK_INT_ISO_STUCK) {
  744. PRINT(KERN_INFO, lynx->id, "isochronous transmitter stuck");
  745. }
  746. if (linkint & LINK_INT_ASYNC_STUCK) {
  747. PRINT(KERN_INFO, lynx->id, "asynchronous transmitter stuck");
  748. }
  749. if (linkint & LINK_INT_SENT_REJECT) {
  750. PRINT(KERN_INFO, lynx->id, "sent reject");
  751. }
  752. if (linkint & LINK_INT_TX_INVALID_TC) {
  753. PRINT(KERN_INFO, lynx->id, "invalid transaction code");
  754. }
  755. if (linkint & LINK_INT_GRF_OVERFLOW) {
  756. /* flush FIFO if overflow happens during reset */
  757. if (host->in_bus_reset)
  758. reg_write(lynx, FIFO_CONTROL,
  759. FIFO_CONTROL_GRF_FLUSH);
  760. PRINT(KERN_INFO, lynx->id, "GRF overflow");
  761. }
  762. if (linkint & LINK_INT_ITF_UNDERFLOW) {
  763. PRINT(KERN_INFO, lynx->id, "ITF underflow");
  764. }
  765. if (linkint & LINK_INT_ATF_UNDERFLOW) {
  766. PRINT(KERN_INFO, lynx->id, "ATF underflow");
  767. }
  768. }
  769. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ISO_RCV)) {
  770. PRINTD(KERN_DEBUG, lynx->id, "iso receive");
  771. spin_lock(&lynx->iso_rcv.lock);
  772. lynx->iso_rcv.stat[lynx->iso_rcv.next] =
  773. reg_read(lynx, DMA_CHAN_STAT(CHANNEL_ISO_RCV));
  774. lynx->iso_rcv.used++;
  775. lynx->iso_rcv.next = (lynx->iso_rcv.next + 1) % NUM_ISORCV_PCL;
  776. if ((lynx->iso_rcv.next == lynx->iso_rcv.last)
  777. || !lynx->iso_rcv.chan_count) {
  778. PRINTD(KERN_DEBUG, lynx->id, "stopped");
  779. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV), 0);
  780. }
  781. run_sub_pcl(lynx, lynx->iso_rcv.pcl_start, lynx->iso_rcv.next,
  782. CHANNEL_ISO_RCV);
  783. spin_unlock(&lynx->iso_rcv.lock);
  784. tasklet_schedule(&lynx->iso_rcv.tq);
  785. }
  786. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ASYNC_SEND)) {
  787. PRINTD(KERN_DEBUG, lynx->id, "async sent");
  788. spin_lock(&lynx->async.queue_lock);
  789. if (list_empty(&lynx->async.pcl_queue)) {
  790. spin_unlock(&lynx->async.queue_lock);
  791. PRINT(KERN_WARNING, lynx->id, "async dma halted, but no queued packet (maybe it was cancelled)");
  792. } else {
  793. struct ti_pcl pcl;
  794. u32 ack;
  795. struct hpsb_packet *packet;
  796. get_pcl(lynx, lynx->async.pcl, &pcl);
  797. packet = driver_packet(lynx->async.pcl_queue.next);
  798. list_del_init(&packet->driver_list);
  799. pci_unmap_single(lynx->dev, lynx->async.header_dma,
  800. packet->header_size, PCI_DMA_TODEVICE);
  801. if (packet->data_size) {
  802. pci_unmap_single(lynx->dev, lynx->async.data_dma,
  803. packet->data_size, PCI_DMA_TODEVICE);
  804. }
  805. if (!list_empty(&lynx->async.queue)) {
  806. send_next(lynx, hpsb_async);
  807. }
  808. spin_unlock(&lynx->async.queue_lock);
  809. if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
  810. if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
  811. ack = (pcl.pcl_status >> 15) & 0xf;
  812. PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
  813. ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
  814. } else {
  815. ack = (pcl.pcl_status >> 15) & 0xf;
  816. }
  817. } else {
  818. PRINT(KERN_INFO, lynx->id, "async packet was not completed");
  819. ack = ACKX_SEND_ERROR;
  820. }
  821. hpsb_packet_sent(host, packet, ack);
  822. }
  823. }
  824. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ISO_SEND)) {
  825. PRINTD(KERN_DEBUG, lynx->id, "iso sent");
  826. spin_lock(&lynx->iso_send.queue_lock);
  827. if (list_empty(&lynx->iso_send.pcl_queue)) {
  828. spin_unlock(&lynx->iso_send.queue_lock);
  829. PRINT(KERN_ERR, lynx->id, "iso send dma halted, but no queued packet");
  830. } else {
  831. struct ti_pcl pcl;
  832. u32 ack;
  833. struct hpsb_packet *packet;
  834. get_pcl(lynx, lynx->iso_send.pcl, &pcl);
  835. packet = driver_packet(lynx->iso_send.pcl_queue.next);
  836. list_del_init(&packet->driver_list);
  837. pci_unmap_single(lynx->dev, lynx->iso_send.header_dma,
  838. packet->header_size, PCI_DMA_TODEVICE);
  839. if (packet->data_size) {
  840. pci_unmap_single(lynx->dev, lynx->iso_send.data_dma,
  841. packet->data_size, PCI_DMA_TODEVICE);
  842. }
  843. #if 0 /* has been removed from ieee1394 core */
  844. if (!list_empty(&lynx->iso_send.queue)) {
  845. send_next(lynx, hpsb_iso);
  846. }
  847. #endif
  848. spin_unlock(&lynx->iso_send.queue_lock);
  849. if (pcl.pcl_status & DMA_CHAN_STAT_PKTCMPL) {
  850. if (pcl.pcl_status & DMA_CHAN_STAT_SPECIALACK) {
  851. ack = (pcl.pcl_status >> 15) & 0xf;
  852. PRINTD(KERN_INFO, lynx->id, "special ack %d", ack);
  853. ack = (ack == 1 ? ACKX_TIMEOUT : ACKX_SEND_ERROR);
  854. } else {
  855. ack = (pcl.pcl_status >> 15) & 0xf;
  856. }
  857. } else {
  858. PRINT(KERN_INFO, lynx->id, "iso send packet was not completed");
  859. ack = ACKX_SEND_ERROR;
  860. }
  861. hpsb_packet_sent(host, packet, ack); //FIXME: maybe we should just use ACK_COMPLETE and ACKX_SEND_ERROR
  862. }
  863. }
  864. if (intmask & PCI_INT_DMA_HLT(CHANNEL_ASYNC_RCV)) {
  865. /* general receive DMA completed */
  866. int stat = reg_read(lynx, DMA_CHAN_STAT(CHANNEL_ASYNC_RCV));
  867. PRINTD(KERN_DEBUG, lynx->id, "received packet size %d",
  868. stat & 0x1fff);
  869. if (stat & DMA_CHAN_STAT_SELFID) {
  870. lynx->selfid_size = stat & 0x1fff;
  871. handle_selfid(lynx, host);
  872. } else {
  873. quadlet_t *q_data = lynx->rcv_page;
  874. if ((*q_data >> 4 & 0xf) == TCODE_READQ_RESPONSE
  875. || (*q_data >> 4 & 0xf) == TCODE_WRITEQ) {
  876. cpu_to_be32s(q_data + 3);
  877. }
  878. hpsb_packet_received(host, q_data, stat & 0x1fff, 0);
  879. }
  880. run_pcl(lynx, lynx->rcv_pcl_start, CHANNEL_ASYNC_RCV);
  881. }
  882. return IRQ_HANDLED;
  883. }
  884. static void iso_rcv_bh(struct ti_lynx *lynx)
  885. {
  886. unsigned int idx;
  887. quadlet_t *data;
  888. unsigned long flags;
  889. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  890. while (lynx->iso_rcv.used) {
  891. idx = lynx->iso_rcv.last;
  892. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  893. data = lynx->iso_rcv.page[idx / ISORCV_PER_PAGE]
  894. + (idx % ISORCV_PER_PAGE) * MAX_ISORCV_SIZE;
  895. if ((*data >> 16) + 4 != (lynx->iso_rcv.stat[idx] & 0x1fff)) {
  896. PRINT(KERN_ERR, lynx->id,
  897. "iso length mismatch 0x%08x/0x%08x", *data,
  898. lynx->iso_rcv.stat[idx]);
  899. }
  900. if (lynx->iso_rcv.stat[idx]
  901. & (DMA_CHAN_STAT_PCIERR | DMA_CHAN_STAT_PKTERR)) {
  902. PRINT(KERN_INFO, lynx->id,
  903. "iso receive error on %d to 0x%p", idx, data);
  904. } else {
  905. hpsb_packet_received(lynx->host, data,
  906. lynx->iso_rcv.stat[idx] & 0x1fff,
  907. 0);
  908. }
  909. spin_lock_irqsave(&lynx->iso_rcv.lock, flags);
  910. lynx->iso_rcv.last = (idx + 1) % NUM_ISORCV_PCL;
  911. lynx->iso_rcv.used--;
  912. }
  913. if (lynx->iso_rcv.chan_count) {
  914. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV),
  915. DMA_WORD1_CMP_ENABLE_MASTER);
  916. }
  917. spin_unlock_irqrestore(&lynx->iso_rcv.lock, flags);
  918. }
  919. static void remove_card(struct pci_dev *dev)
  920. {
  921. struct ti_lynx *lynx;
  922. struct device *lynx_dev;
  923. int i;
  924. lynx = pci_get_drvdata(dev);
  925. if (!lynx) return;
  926. pci_set_drvdata(dev, NULL);
  927. lynx_dev = get_device(&lynx->host->device);
  928. switch (lynx->state) {
  929. case is_host:
  930. reg_write(lynx, PCI_INT_ENABLE, 0);
  931. hpsb_remove_host(lynx->host);
  932. case have_intr:
  933. reg_write(lynx, PCI_INT_ENABLE, 0);
  934. free_irq(lynx->dev->irq, lynx);
  935. /* Disable IRM Contender and LCtrl */
  936. if (lynx->phyic.reg_1394a)
  937. set_phy_reg(lynx, 4, ~0xc0 & get_phy_reg(lynx, 4));
  938. /* Let all other nodes know to ignore us */
  939. lynx_devctl(lynx->host, RESET_BUS, LONG_RESET_NO_FORCE_ROOT);
  940. case have_iomappings:
  941. reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
  942. /* Fix buggy cards with autoboot pin not tied low: */
  943. reg_write(lynx, DMA0_CHAN_CTRL, 0);
  944. iounmap(lynx->registers);
  945. iounmap(lynx->local_rom);
  946. iounmap(lynx->local_ram);
  947. iounmap(lynx->aux_port);
  948. case have_1394_buffers:
  949. for (i = 0; i < ISORCV_PAGES; i++) {
  950. if (lynx->iso_rcv.page[i]) {
  951. pci_free_consistent(lynx->dev, PAGE_SIZE,
  952. lynx->iso_rcv.page[i],
  953. lynx->iso_rcv.page_dma[i]);
  954. }
  955. }
  956. pci_free_consistent(lynx->dev, PAGE_SIZE, lynx->rcv_page,
  957. lynx->rcv_page_dma);
  958. case have_aux_buf:
  959. case have_pcl_mem:
  960. pci_free_consistent(lynx->dev, LOCALRAM_SIZE, lynx->pcl_mem,
  961. lynx->pcl_mem_dma);
  962. case clear:
  963. /* do nothing - already freed */
  964. ;
  965. }
  966. tasklet_kill(&lynx->iso_rcv.tq);
  967. if (lynx_dev)
  968. put_device(lynx_dev);
  969. }
  970. static int __devinit add_card(struct pci_dev *dev,
  971. const struct pci_device_id *devid_is_unused)
  972. {
  973. #define FAIL(fmt, args...) do { \
  974. PRINT_G(KERN_ERR, fmt , ## args); \
  975. remove_card(dev); \
  976. return error; \
  977. } while (0)
  978. char irq_buf[16];
  979. struct hpsb_host *host;
  980. struct ti_lynx *lynx; /* shortcut to currently handled device */
  981. struct ti_pcl pcl;
  982. u32 *pcli;
  983. int i;
  984. int error;
  985. error = -ENXIO;
  986. if (pci_set_dma_mask(dev, DMA_32BIT_MASK))
  987. FAIL("DMA address limits not supported for PCILynx hardware");
  988. if (pci_enable_device(dev))
  989. FAIL("failed to enable PCILynx hardware");
  990. pci_set_master(dev);
  991. error = -ENOMEM;
  992. host = hpsb_alloc_host(&lynx_driver, sizeof(struct ti_lynx), &dev->dev);
  993. if (!host) FAIL("failed to allocate control structure memory");
  994. lynx = host->hostdata;
  995. lynx->id = card_id++;
  996. lynx->dev = dev;
  997. lynx->state = clear;
  998. lynx->host = host;
  999. host->pdev = dev;
  1000. pci_set_drvdata(dev, lynx);
  1001. spin_lock_init(&lynx->lock);
  1002. spin_lock_init(&lynx->phy_reg_lock);
  1003. lynx->pcl_mem = pci_alloc_consistent(dev, LOCALRAM_SIZE,
  1004. &lynx->pcl_mem_dma);
  1005. if (lynx->pcl_mem != NULL) {
  1006. lynx->state = have_pcl_mem;
  1007. PRINT(KERN_INFO, lynx->id,
  1008. "allocated PCL memory %d Bytes @ 0x%p", LOCALRAM_SIZE,
  1009. lynx->pcl_mem);
  1010. } else {
  1011. FAIL("failed to allocate PCL memory area");
  1012. }
  1013. lynx->rcv_page = pci_alloc_consistent(dev, PAGE_SIZE,
  1014. &lynx->rcv_page_dma);
  1015. if (lynx->rcv_page == NULL) {
  1016. FAIL("failed to allocate receive buffer");
  1017. }
  1018. lynx->state = have_1394_buffers;
  1019. for (i = 0; i < ISORCV_PAGES; i++) {
  1020. lynx->iso_rcv.page[i] =
  1021. pci_alloc_consistent(dev, PAGE_SIZE,
  1022. &lynx->iso_rcv.page_dma[i]);
  1023. if (lynx->iso_rcv.page[i] == NULL) {
  1024. FAIL("failed to allocate iso receive buffers");
  1025. }
  1026. }
  1027. lynx->registers = ioremap_nocache(pci_resource_start(dev,0),
  1028. PCILYNX_MAX_REGISTER);
  1029. lynx->local_ram = ioremap(pci_resource_start(dev,1), PCILYNX_MAX_MEMORY);
  1030. lynx->aux_port = ioremap(pci_resource_start(dev,2), PCILYNX_MAX_MEMORY);
  1031. lynx->local_rom = ioremap(pci_resource_start(dev,PCI_ROM_RESOURCE),
  1032. PCILYNX_MAX_MEMORY);
  1033. lynx->state = have_iomappings;
  1034. if (lynx->registers == NULL) {
  1035. FAIL("failed to remap registers - card not accessible");
  1036. }
  1037. reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET);
  1038. /* Fix buggy cards with autoboot pin not tied low: */
  1039. reg_write(lynx, DMA0_CHAN_CTRL, 0);
  1040. sprintf (irq_buf, "%d", dev->irq);
  1041. if (!request_irq(dev->irq, lynx_irq_handler, IRQF_SHARED,
  1042. PCILYNX_DRIVER_NAME, lynx)) {
  1043. PRINT(KERN_INFO, lynx->id, "allocated interrupt %s", irq_buf);
  1044. lynx->state = have_intr;
  1045. } else {
  1046. FAIL("failed to allocate shared interrupt %s", irq_buf);
  1047. }
  1048. /* alloc_pcl return values are not checked, it is expected that the
  1049. * provided PCL space is sufficient for the initial allocations */
  1050. lynx->rcv_pcl = alloc_pcl(lynx);
  1051. lynx->rcv_pcl_start = alloc_pcl(lynx);
  1052. lynx->async.pcl = alloc_pcl(lynx);
  1053. lynx->async.pcl_start = alloc_pcl(lynx);
  1054. lynx->iso_send.pcl = alloc_pcl(lynx);
  1055. lynx->iso_send.pcl_start = alloc_pcl(lynx);
  1056. for (i = 0; i < NUM_ISORCV_PCL; i++) {
  1057. lynx->iso_rcv.pcl[i] = alloc_pcl(lynx);
  1058. }
  1059. lynx->iso_rcv.pcl_start = alloc_pcl(lynx);
  1060. /* all allocations successful - simple init stuff follows */
  1061. reg_write(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL);
  1062. tasklet_init(&lynx->iso_rcv.tq, (void (*)(unsigned long))iso_rcv_bh,
  1063. (unsigned long)lynx);
  1064. spin_lock_init(&lynx->iso_rcv.lock);
  1065. spin_lock_init(&lynx->async.queue_lock);
  1066. lynx->async.channel = CHANNEL_ASYNC_SEND;
  1067. spin_lock_init(&lynx->iso_send.queue_lock);
  1068. lynx->iso_send.channel = CHANNEL_ISO_SEND;
  1069. PRINT(KERN_INFO, lynx->id, "remapped memory spaces reg 0x%p, rom 0x%p, "
  1070. "ram 0x%p, aux 0x%p", lynx->registers, lynx->local_rom,
  1071. lynx->local_ram, lynx->aux_port);
  1072. /* now, looking for PHY register set */
  1073. if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) {
  1074. lynx->phyic.reg_1394a = 1;
  1075. PRINT(KERN_INFO, lynx->id,
  1076. "found 1394a conform PHY (using extended register set)");
  1077. lynx->phyic.vendor = get_phy_vendorid(lynx);
  1078. lynx->phyic.product = get_phy_productid(lynx);
  1079. } else {
  1080. lynx->phyic.reg_1394a = 0;
  1081. PRINT(KERN_INFO, lynx->id, "found old 1394 PHY");
  1082. }
  1083. lynx->selfid_size = -1;
  1084. lynx->phy_reg0 = -1;
  1085. INIT_LIST_HEAD(&lynx->async.queue);
  1086. INIT_LIST_HEAD(&lynx->async.pcl_queue);
  1087. INIT_LIST_HEAD(&lynx->iso_send.queue);
  1088. INIT_LIST_HEAD(&lynx->iso_send.pcl_queue);
  1089. pcl.next = pcl_bus(lynx, lynx->rcv_pcl);
  1090. put_pcl(lynx, lynx->rcv_pcl_start, &pcl);
  1091. pcl.next = PCL_NEXT_INVALID;
  1092. pcl.async_error_next = PCL_NEXT_INVALID;
  1093. pcl.buffer[0].control = PCL_CMD_RCV | 16;
  1094. #ifndef __BIG_ENDIAN
  1095. pcl.buffer[0].control |= PCL_BIGENDIAN;
  1096. #endif
  1097. pcl.buffer[1].control = PCL_LAST_BUFF | 4080;
  1098. pcl.buffer[0].pointer = lynx->rcv_page_dma;
  1099. pcl.buffer[1].pointer = lynx->rcv_page_dma + 16;
  1100. put_pcl(lynx, lynx->rcv_pcl, &pcl);
  1101. pcl.next = pcl_bus(lynx, lynx->async.pcl);
  1102. pcl.async_error_next = pcl_bus(lynx, lynx->async.pcl);
  1103. put_pcl(lynx, lynx->async.pcl_start, &pcl);
  1104. pcl.next = pcl_bus(lynx, lynx->iso_send.pcl);
  1105. pcl.async_error_next = PCL_NEXT_INVALID;
  1106. put_pcl(lynx, lynx->iso_send.pcl_start, &pcl);
  1107. pcl.next = PCL_NEXT_INVALID;
  1108. pcl.async_error_next = PCL_NEXT_INVALID;
  1109. pcl.buffer[0].control = PCL_CMD_RCV | 4;
  1110. #ifndef __BIG_ENDIAN
  1111. pcl.buffer[0].control |= PCL_BIGENDIAN;
  1112. #endif
  1113. pcl.buffer[1].control = PCL_LAST_BUFF | 2044;
  1114. for (i = 0; i < NUM_ISORCV_PCL; i++) {
  1115. int page = i / ISORCV_PER_PAGE;
  1116. int sec = i % ISORCV_PER_PAGE;
  1117. pcl.buffer[0].pointer = lynx->iso_rcv.page_dma[page]
  1118. + sec * MAX_ISORCV_SIZE;
  1119. pcl.buffer[1].pointer = pcl.buffer[0].pointer + 4;
  1120. put_pcl(lynx, lynx->iso_rcv.pcl[i], &pcl);
  1121. }
  1122. pcli = (u32 *)&pcl;
  1123. for (i = 0; i < NUM_ISORCV_PCL; i++) {
  1124. pcli[i] = pcl_bus(lynx, lynx->iso_rcv.pcl[i]);
  1125. }
  1126. put_pcl(lynx, lynx->iso_rcv.pcl_start, &pcl);
  1127. /* FIFO sizes from left to right: ITF=48 ATF=48 GRF=160 */
  1128. reg_write(lynx, FIFO_SIZES, 0x003030a0);
  1129. /* 20 byte threshold before triggering PCI transfer */
  1130. reg_write(lynx, DMA_GLOBAL_REGISTER, 0x2<<24);
  1131. /* threshold on both send FIFOs before transmitting:
  1132. FIFO size - cache line size - 1 */
  1133. i = reg_read(lynx, PCI_LATENCY_CACHELINE) & 0xff;
  1134. i = 0x30 - i - 1;
  1135. reg_write(lynx, FIFO_XMIT_THRESHOLD, (i << 8) | i);
  1136. reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_1394);
  1137. reg_write(lynx, LINK_INT_ENABLE, LINK_INT_PHY_TIMEOUT
  1138. | LINK_INT_PHY_REG_RCVD | LINK_INT_PHY_BUSRESET
  1139. | LINK_INT_ISO_STUCK | LINK_INT_ASYNC_STUCK
  1140. | LINK_INT_SENT_REJECT | LINK_INT_TX_INVALID_TC
  1141. | LINK_INT_GRF_OVERFLOW | LINK_INT_ITF_UNDERFLOW
  1142. | LINK_INT_ATF_UNDERFLOW);
  1143. reg_write(lynx, DMA_WORD0_CMP_VALUE(CHANNEL_ASYNC_RCV), 0);
  1144. reg_write(lynx, DMA_WORD0_CMP_ENABLE(CHANNEL_ASYNC_RCV), 0xa<<4);
  1145. reg_write(lynx, DMA_WORD1_CMP_VALUE(CHANNEL_ASYNC_RCV), 0);
  1146. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ASYNC_RCV),
  1147. DMA_WORD1_CMP_MATCH_LOCAL_NODE | DMA_WORD1_CMP_MATCH_BROADCAST
  1148. | DMA_WORD1_CMP_MATCH_EXACT | DMA_WORD1_CMP_MATCH_BUS_BCAST
  1149. | DMA_WORD1_CMP_ENABLE_SELF_ID | DMA_WORD1_CMP_ENABLE_MASTER);
  1150. run_pcl(lynx, lynx->rcv_pcl_start, CHANNEL_ASYNC_RCV);
  1151. reg_write(lynx, DMA_WORD0_CMP_VALUE(CHANNEL_ISO_RCV), 0);
  1152. reg_write(lynx, DMA_WORD0_CMP_ENABLE(CHANNEL_ISO_RCV), 0x9<<4);
  1153. reg_write(lynx, DMA_WORD1_CMP_VALUE(CHANNEL_ISO_RCV), 0);
  1154. reg_write(lynx, DMA_WORD1_CMP_ENABLE(CHANNEL_ISO_RCV), 0);
  1155. run_sub_pcl(lynx, lynx->iso_rcv.pcl_start, 0, CHANNEL_ISO_RCV);
  1156. reg_write(lynx, LINK_CONTROL, LINK_CONTROL_RCV_CMP_VALID
  1157. | LINK_CONTROL_TX_ISO_EN | LINK_CONTROL_RX_ISO_EN
  1158. | LINK_CONTROL_TX_ASYNC_EN | LINK_CONTROL_RX_ASYNC_EN
  1159. | LINK_CONTROL_RESET_TX | LINK_CONTROL_RESET_RX);
  1160. if (!lynx->phyic.reg_1394a) {
  1161. if (!hpsb_disable_irm) {
  1162. /* attempt to enable contender bit -FIXME- would this
  1163. * work elsewhere? */
  1164. reg_set_bits(lynx, GPIO_CTRL_A, 0x1);
  1165. reg_write(lynx, GPIO_DATA_BASE + 0x3c, 0x1);
  1166. }
  1167. } else {
  1168. /* set the contender (if appropriate) and LCtrl bit in the
  1169. * extended PHY register set. (Should check that PHY_02_EXTENDED
  1170. * is set in register 2?)
  1171. */
  1172. i = get_phy_reg(lynx, 4);
  1173. i |= PHY_04_LCTRL;
  1174. if (hpsb_disable_irm)
  1175. i &= ~PHY_04_CONTENDER;
  1176. else
  1177. i |= PHY_04_CONTENDER;
  1178. if (i != -1) set_phy_reg(lynx, 4, i);
  1179. }
  1180. if (!skip_eeprom)
  1181. {
  1182. /* needed for i2c communication with serial eeprom */
  1183. struct i2c_adapter *i2c_ad;
  1184. struct i2c_algo_bit_data i2c_adapter_data;
  1185. error = -ENOMEM;
  1186. i2c_ad = kmemdup(&bit_ops, sizeof(*i2c_ad), GFP_KERNEL);
  1187. if (!i2c_ad) FAIL("failed to allocate I2C adapter memory");
  1188. i2c_adapter_data = bit_data;
  1189. i2c_ad->algo_data = &i2c_adapter_data;
  1190. i2c_adapter_data.data = lynx;
  1191. i2c_ad->dev.parent = &dev->dev;
  1192. PRINTD(KERN_DEBUG, lynx->id,"original eeprom control: %d",
  1193. reg_read(lynx, SERIAL_EEPROM_CONTROL));
  1194. /* reset hardware to sane state */
  1195. lynx->i2c_driven_state = 0x00000070;
  1196. reg_write(lynx, SERIAL_EEPROM_CONTROL, lynx->i2c_driven_state);
  1197. if (i2c_bit_add_bus(i2c_ad) < 0)
  1198. {
  1199. kfree(i2c_ad);
  1200. error = -ENXIO;
  1201. FAIL("unable to register i2c");
  1202. }
  1203. else
  1204. {
  1205. /* do i2c stuff */
  1206. unsigned char i2c_cmd = 0x10;
  1207. struct i2c_msg msg[2] = { { 0x50, 0, 1, &i2c_cmd },
  1208. { 0x50, I2C_M_RD, 20, (unsigned char*) lynx->bus_info_block }
  1209. };
  1210. /* we use i2c_transfer, because i2c_smbus_read_block_data does not work properly and we
  1211. do it more efficiently in one transaction rather then using several reads */
  1212. if (i2c_transfer(i2c_ad, msg, 2) < 0) {
  1213. PRINT(KERN_ERR, lynx->id, "unable to read bus info block from i2c");
  1214. } else {
  1215. int i;
  1216. PRINT(KERN_INFO, lynx->id, "got bus info block from serial eeprom");
  1217. /* FIXME: probably we shoud rewrite the max_rec, max_ROM(1394a),
  1218. * generation(1394a) and link_spd(1394a) field and recalculate
  1219. * the CRC */
  1220. for (i = 0; i < 5 ; i++)
  1221. PRINTD(KERN_DEBUG, lynx->id, "Businfo block quadlet %i: %08x",
  1222. i, be32_to_cpu(lynx->bus_info_block[i]));
  1223. /* info_length, crc_length and 1394 magic number to check, if it is really a bus info block */
  1224. if (((be32_to_cpu(lynx->bus_info_block[0]) & 0xffff0000) == 0x04040000) &&
  1225. (lynx->bus_info_block[1] == __constant_cpu_to_be32(0x31333934)))
  1226. {
  1227. PRINT(KERN_DEBUG, lynx->id, "read a valid bus info block from");
  1228. } else {
  1229. kfree(i2c_ad);
  1230. error = -ENXIO;
  1231. FAIL("read something from serial eeprom, but it does not seem to be a valid bus info block");
  1232. }
  1233. }
  1234. i2c_del_adapter(i2c_ad);
  1235. kfree(i2c_ad);
  1236. }
  1237. }
  1238. host->csr.guid_hi = be32_to_cpu(lynx->bus_info_block[3]);
  1239. host->csr.guid_lo = be32_to_cpu(lynx->bus_info_block[4]);
  1240. host->csr.cyc_clk_acc = (be32_to_cpu(lynx->bus_info_block[2]) >> 16) & 0xff;
  1241. host->csr.max_rec = (be32_to_cpu(lynx->bus_info_block[2]) >> 12) & 0xf;
  1242. if (!lynx->phyic.reg_1394a)
  1243. host->csr.lnk_spd = (get_phy_reg(lynx, 2) & 0xc0) >> 6;
  1244. else
  1245. host->csr.lnk_spd = be32_to_cpu(lynx->bus_info_block[2]) & 0x7;
  1246. if (hpsb_add_host(host)) {
  1247. error = -ENOMEM;
  1248. FAIL("Failed to register host with highlevel");
  1249. }
  1250. lynx->state = is_host;
  1251. return 0;
  1252. #undef FAIL
  1253. }
  1254. static struct pci_device_id pci_table[] = {
  1255. {
  1256. .vendor = PCI_VENDOR_ID_TI,
  1257. .device = PCI_DEVICE_ID_TI_PCILYNX,
  1258. .subvendor = PCI_ANY_ID,
  1259. .subdevice = PCI_ANY_ID,
  1260. },
  1261. { } /* Terminating entry */
  1262. };
  1263. static struct pci_driver lynx_pci_driver = {
  1264. .name = PCILYNX_DRIVER_NAME,
  1265. .id_table = pci_table,
  1266. .probe = add_card,
  1267. .remove = remove_card,
  1268. };
  1269. static struct hpsb_host_driver lynx_driver = {
  1270. .owner = THIS_MODULE,
  1271. .name = PCILYNX_DRIVER_NAME,
  1272. .set_hw_config_rom = NULL,
  1273. .transmit_packet = lynx_transmit,
  1274. .devctl = lynx_devctl,
  1275. .isoctl = NULL,
  1276. };
  1277. MODULE_AUTHOR("Andreas E. Bombe <andreas.bombe@munich.netsurf.de>");
  1278. MODULE_DESCRIPTION("driver for Texas Instruments PCI Lynx IEEE-1394 controller");
  1279. MODULE_LICENSE("GPL");
  1280. MODULE_SUPPORTED_DEVICE("pcilynx");
  1281. MODULE_DEVICE_TABLE(pci, pci_table);
  1282. static int __init pcilynx_init(void)
  1283. {
  1284. int ret;
  1285. ret = pci_register_driver(&lynx_pci_driver);
  1286. if (ret < 0) {
  1287. PRINT_G(KERN_ERR, "PCI module init failed");
  1288. return ret;
  1289. }
  1290. return 0;
  1291. }
  1292. static void __exit pcilynx_cleanup(void)
  1293. {
  1294. pci_unregister_driver(&lynx_pci_driver);
  1295. }
  1296. module_init(pcilynx_init);
  1297. module_exit(pcilynx_cleanup);