sh_eth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * sh_eth.c - Driver for Renesas SH7763's ethernet controler.
  3. *
  4. * Copyright (C) 2008 Renesas Solutions Corp.
  5. * Copyright (c) 2008 Nobuhiro Iwamatsu
  6. * Copyright (c) 2007 Carlos Munoz <carlos@kenati.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <common.h>
  24. #include <malloc.h>
  25. #include <net.h>
  26. #include <asm/errno.h>
  27. #include <asm/io.h>
  28. #include "sh_eth.h"
  29. #ifndef CONFIG_SH_ETHER_USE_PORT
  30. # error "Please define CONFIG_SH_ETHER_USE_PORT"
  31. #endif
  32. #ifndef CONFIG_SH_ETHER_PHY_ADDR
  33. # error "Please define CONFIG_SH_ETHER_PHY_ADDR"
  34. #endif
  35. extern int eth_init(bd_t *bd);
  36. extern void eth_halt(void);
  37. extern int eth_rx(void);
  38. extern int eth_send(volatile void *packet, int length);
  39. static struct dev_info_s *dev;
  40. /*
  41. * Bits are written to the PHY serially using the
  42. * PIR register, just like a bit banger.
  43. */
  44. static void sh_eth_mii_write_phy_bits(int port, u32 val, int len)
  45. {
  46. int i;
  47. u32 pir;
  48. /* Bit positions is 1 less than the number of bits */
  49. for (i = len - 1; i >= 0; i--) {
  50. /* Write direction, bit to write, clock is low */
  51. pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
  52. outl(pir, PIR(port));
  53. udelay(1);
  54. /* Write direction, bit to write, clock is high */
  55. pir = 3 | ((val & 1 << i) ? 1 << 2 : 0);
  56. outl(pir, PIR(port));
  57. udelay(1);
  58. /* Write direction, bit to write, clock is low */
  59. pir = 2 | ((val & 1 << i) ? 1 << 2 : 0);
  60. outl(pir, PIR(port));
  61. udelay(1);
  62. }
  63. }
  64. static void sh_eth_mii_bus_release(int port)
  65. {
  66. /* Read direction, clock is low */
  67. outl(0, PIR(port));
  68. udelay(1);
  69. /* Read direction, clock is high */
  70. outl(1, PIR(port));
  71. udelay(1);
  72. /* Read direction, clock is low */
  73. outl(0, PIR(port));
  74. udelay(1);
  75. }
  76. static void sh_eth_mii_ind_bus_release(int port)
  77. {
  78. /* Read direction, clock is low */
  79. outl(0, PIR(port));
  80. udelay(1);
  81. }
  82. static int sh_eth_mii_read_phy_bits(int port, u32 * val, int len)
  83. {
  84. int i;
  85. u32 pir;
  86. *val = 0;
  87. for (i = len - 1; i >= 0; i--) {
  88. /* Read direction, clock is high */
  89. outl(1, PIR(port));
  90. udelay(1);
  91. /* Read bit */
  92. pir = inl(PIR(port));
  93. *val |= (pir & 8) ? 1 << i : 0;
  94. /* Read direction, clock is low */
  95. outl(0, PIR(port));
  96. udelay(1);
  97. }
  98. return 0;
  99. }
  100. #define PHY_INIT 0xFFFFFFFF
  101. #define PHY_READ 0x02
  102. #define PHY_WRITE 0x01
  103. /*
  104. * To read a phy register, mii managements frames are sent to the phy.
  105. * The frames look like this:
  106. * pre (32 bits): 0xffff ffff
  107. * st (2 bits): 01
  108. * op (2bits): 10: read 01: write
  109. * phyad (5 bits): xxxxx
  110. * regad (5 bits): xxxxx
  111. * ta (Bus release):
  112. * data (16 bits): read data
  113. */
  114. static u32 sh_eth_mii_read_phy_reg(int port, u8 phy_addr, int reg)
  115. {
  116. u32 val;
  117. /* Sent mii management frame */
  118. /* pre */
  119. sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
  120. /* st (start of frame) */
  121. sh_eth_mii_write_phy_bits(port, 0x1, 2);
  122. /* op (code) */
  123. sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
  124. /* phy address */
  125. sh_eth_mii_write_phy_bits(port, phy_addr, 5);
  126. /* Register to read */
  127. sh_eth_mii_write_phy_bits(port, reg, 5);
  128. /* Bus release */
  129. sh_eth_mii_bus_release(port);
  130. /* Read register */
  131. sh_eth_mii_read_phy_bits(port, &val, 16);
  132. return val;
  133. }
  134. /*
  135. * To write a phy register, mii managements frames are sent to the phy.
  136. * The frames look like this:
  137. * pre (32 bits): 0xffff ffff
  138. * st (2 bits): 01
  139. * op (2bits): 10: read 01: write
  140. * phyad (5 bits): xxxxx
  141. * regad (5 bits): xxxxx
  142. * ta (2 bits): 10
  143. * data (16 bits): write data
  144. * idle (Independent bus release)
  145. */
  146. static void sh_eth_mii_write_phy_reg(int port, u8 phy_addr, int reg, u16 val)
  147. {
  148. /* Sent mii management frame */
  149. /* pre */
  150. sh_eth_mii_write_phy_bits(port, PHY_INIT, 32);
  151. /* st (start of frame) */
  152. sh_eth_mii_write_phy_bits(port, 0x1, 2);
  153. /* op (code) */
  154. sh_eth_mii_write_phy_bits(port, PHY_WRITE, 2);
  155. /* phy address */
  156. sh_eth_mii_write_phy_bits(port, phy_addr, 5);
  157. /* Register to read */
  158. sh_eth_mii_write_phy_bits(port, reg, 5);
  159. /* ta */
  160. sh_eth_mii_write_phy_bits(port, PHY_READ, 2);
  161. /* Write register data */
  162. sh_eth_mii_write_phy_bits(port, val, 16);
  163. /* Independent bus release */
  164. sh_eth_mii_ind_bus_release(port);
  165. }
  166. void eth_halt(void)
  167. {
  168. }
  169. int eth_send(volatile void *packet, int len)
  170. {
  171. int port = dev->port;
  172. struct port_info_s *port_info = &dev->port_info[port];
  173. int timeout;
  174. int rc = 0;
  175. if (!packet || len > 0xffff) {
  176. printf("eth_send: Invalid argument\n");
  177. return -EINVAL;
  178. }
  179. /* packet must be a 4 byte boundary */
  180. if ((int)packet & (4 - 1)) {
  181. printf("eth_send: packet not 4 byte alligned\n");
  182. return -EFAULT;
  183. }
  184. /* Update tx descriptor */
  185. port_info->tx_desc_cur->td2 = ADDR_TO_PHY(packet);
  186. port_info->tx_desc_cur->td1 = len << 16;
  187. /* Must preserve the end of descriptor list indication */
  188. if (port_info->tx_desc_cur->td0 & TD_TDLE)
  189. port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP | TD_TDLE;
  190. else
  191. port_info->tx_desc_cur->td0 = TD_TACT | TD_TFP;
  192. /* Restart the transmitter if disabled */
  193. if (!(inl(EDTRR(port)) & EDTRR_TRNS))
  194. outl(EDTRR_TRNS, EDTRR(port));
  195. /* Wait until packet is transmitted */
  196. timeout = 1000;
  197. while (port_info->tx_desc_cur->td0 & TD_TACT && timeout--)
  198. udelay(100);
  199. if (timeout < 0) {
  200. printf("eth_send: transmit timeout\n");
  201. rc = -1;
  202. goto err;
  203. }
  204. err:
  205. port_info->tx_desc_cur++;
  206. if (port_info->tx_desc_cur >= port_info->tx_desc_base + NUM_TX_DESC)
  207. port_info->tx_desc_cur = port_info->tx_desc_base;
  208. return rc;
  209. }
  210. int eth_rx(void)
  211. {
  212. int port = dev->port;
  213. struct port_info_s *port_info = &dev->port_info[port];
  214. int len = 0;
  215. volatile u8 *packet;
  216. /* Check if the rx descriptor is ready */
  217. if (!(port_info->rx_desc_cur->rd0 & RD_RACT)) {
  218. /* Check for errors */
  219. if (!(port_info->rx_desc_cur->rd0 & RD_RFE)) {
  220. len = port_info->rx_desc_cur->rd1 & 0xffff;
  221. packet = (volatile u8 *)
  222. ADDR_TO_P2(port_info->rx_desc_cur->rd2);
  223. NetReceive(packet, len);
  224. }
  225. /* Make current descriptor available again */
  226. if (port_info->rx_desc_cur->rd0 & RD_RDLE)
  227. port_info->rx_desc_cur->rd0 = RD_RACT | RD_RDLE;
  228. else
  229. port_info->rx_desc_cur->rd0 = RD_RACT;
  230. /* Point to the next descriptor */
  231. port_info->rx_desc_cur++;
  232. if (port_info->rx_desc_cur >=
  233. port_info->rx_desc_base + NUM_RX_DESC)
  234. port_info->rx_desc_cur = port_info->rx_desc_base;
  235. }
  236. /* Restart the receiver if disabled */
  237. if (!(inl(EDRRR(port)) & EDRRR_R))
  238. outl(EDRRR_R, EDRRR(port));
  239. return len;
  240. }
  241. #define EDMR_INIT_CNT 1000
  242. static int sh_eth_reset(struct dev_info_s *dev)
  243. {
  244. int port = dev->port;
  245. int i;
  246. /* Start e-dmac transmitter and receiver */
  247. outl(EDSR_ENALL, EDSR(port));
  248. /* Perform a software reset and wait for it to complete */
  249. outl(EDMR_SRST, EDMR(port));
  250. for (i = 0; i < EDMR_INIT_CNT; i++) {
  251. if (!(inl(EDMR(port)) & EDMR_SRST))
  252. break;
  253. udelay(1000);
  254. }
  255. if (i == EDMR_INIT_CNT) {
  256. printf("Error: Software reset timeout\n");
  257. return -1;
  258. }
  259. return 0;
  260. }
  261. static int sh_eth_tx_desc_init(struct dev_info_s *dev)
  262. {
  263. int port = dev->port;
  264. struct port_info_s *port_info = &dev->port_info[port];
  265. u32 tmp_addr;
  266. struct tx_desc_s *cur_tx_desc;
  267. int i;
  268. /* Allocate tx descriptors. They must be TX_DESC_SIZE bytes
  269. aligned */
  270. if (!(port_info->tx_desc_malloc = malloc(NUM_TX_DESC *
  271. sizeof(struct tx_desc_s) +
  272. TX_DESC_SIZE - 1))) {
  273. printf("Error: malloc failed\n");
  274. return -ENOMEM;
  275. }
  276. tmp_addr = (u32) (((int)port_info->tx_desc_malloc + TX_DESC_SIZE - 1) &
  277. ~(TX_DESC_SIZE - 1));
  278. /* Make sure we use a P2 address (non-cacheable) */
  279. port_info->tx_desc_base = (struct tx_desc_s *)ADDR_TO_P2(tmp_addr);
  280. port_info->tx_desc_cur = port_info->tx_desc_base;
  281. /* Initialize all descriptors */
  282. for (cur_tx_desc = port_info->tx_desc_base, i = 0; i < NUM_TX_DESC;
  283. cur_tx_desc++, i++) {
  284. cur_tx_desc->td0 = 0x00;
  285. cur_tx_desc->td1 = 0x00;
  286. cur_tx_desc->td2 = 0x00;
  287. }
  288. /* Mark the end of the descriptors */
  289. cur_tx_desc--;
  290. cur_tx_desc->td0 |= TD_TDLE;
  291. /* Point the controller to the tx descriptor list. Must use physical
  292. addresses */
  293. outl(ADDR_TO_PHY(port_info->tx_desc_base), TDLAR(port));
  294. outl(ADDR_TO_PHY(port_info->tx_desc_base), TDFAR(port));
  295. outl(ADDR_TO_PHY(cur_tx_desc), TDFXR(port));
  296. outl(0x01, TDFFR(port));/* Last discriptor bit */
  297. return 0;
  298. }
  299. static int sh_eth_rx_desc_init(struct dev_info_s *dev)
  300. {
  301. int port = dev->port;
  302. struct port_info_s *port_info = &dev->port_info[port];
  303. u32 tmp_addr;
  304. struct rx_desc_s *cur_rx_desc;
  305. u8 *rx_buf;
  306. int i;
  307. /* Allocate rx descriptors. They must be RX_DESC_SIZE bytes
  308. aligned */
  309. if (!(port_info->rx_desc_malloc = malloc(NUM_RX_DESC *
  310. sizeof(struct rx_desc_s) +
  311. RX_DESC_SIZE - 1))) {
  312. printf("Error: malloc failed\n");
  313. return -ENOMEM;
  314. }
  315. tmp_addr = (u32) (((int)port_info->rx_desc_malloc + RX_DESC_SIZE - 1) &
  316. ~(RX_DESC_SIZE - 1));
  317. /* Make sure we use a P2 address (non-cacheable) */
  318. port_info->rx_desc_base = (struct rx_desc_s *)ADDR_TO_P2(tmp_addr);
  319. port_info->rx_desc_cur = port_info->rx_desc_base;
  320. /* Allocate rx data buffers. They must be 32 bytes aligned and in
  321. P2 area */
  322. if (!(port_info->rx_buf_malloc = malloc(NUM_RX_DESC * MAX_BUF_SIZE +
  323. 31))) {
  324. printf("Error: malloc failed\n");
  325. free(port_info->rx_desc_malloc);
  326. port_info->rx_desc_malloc = NULL;
  327. return -ENOMEM;
  328. }
  329. tmp_addr = (u32)(((int)port_info->rx_buf_malloc + (32 - 1)) &
  330. ~(32 - 1));
  331. port_info->rx_buf_base = (u8 *)ADDR_TO_P2(tmp_addr);
  332. /* Initialize all descriptors */
  333. for (cur_rx_desc = port_info->rx_desc_base,
  334. rx_buf = port_info->rx_buf_base, i = 0;
  335. i < NUM_RX_DESC; cur_rx_desc++, rx_buf += MAX_BUF_SIZE, i++) {
  336. cur_rx_desc->rd0 = RD_RACT;
  337. cur_rx_desc->rd1 = MAX_BUF_SIZE << 16;
  338. cur_rx_desc->rd2 = (u32) ADDR_TO_PHY(rx_buf);
  339. }
  340. /* Mark the end of the descriptors */
  341. cur_rx_desc--;
  342. cur_rx_desc->rd0 |= RD_RDLE;
  343. /* Point the controller to the rx descriptor list */
  344. outl(ADDR_TO_PHY(port_info->rx_desc_base), RDLAR(port));
  345. outl(ADDR_TO_PHY(port_info->rx_desc_base), RDFAR(port));
  346. outl(ADDR_TO_PHY(cur_rx_desc), RDFXR(port));
  347. outl(RDFFR_RDLF, RDFFR(port));
  348. return 0;
  349. }
  350. static void sh_eth_desc_free(struct dev_info_s *dev)
  351. {
  352. int port = dev->port;
  353. struct port_info_s *port_info = &dev->port_info[port];
  354. if (port_info->tx_desc_malloc) {
  355. free(port_info->tx_desc_malloc);
  356. port_info->tx_desc_malloc = NULL;
  357. }
  358. if (port_info->rx_desc_malloc) {
  359. free(port_info->rx_desc_malloc);
  360. port_info->rx_desc_malloc = NULL;
  361. }
  362. if (port_info->rx_buf_malloc) {
  363. free(port_info->rx_buf_malloc);
  364. port_info->rx_buf_malloc = NULL;
  365. }
  366. }
  367. static int sh_eth_desc_init(struct dev_info_s *dev)
  368. {
  369. int rc;
  370. if ((rc = sh_eth_tx_desc_init(dev)) || (rc = sh_eth_rx_desc_init(dev))) {
  371. sh_eth_desc_free(dev);
  372. return rc;
  373. }
  374. return 0;
  375. }
  376. static int sh_eth_phy_config(struct dev_info_s *dev)
  377. {
  378. int port = dev->port;
  379. struct port_info_s *port_info = &dev->port_info[port];
  380. int timeout;
  381. u32 val;
  382. /* Reset phy */
  383. sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL, PHY_C_RESET);
  384. timeout = 10;
  385. while (timeout--) {
  386. val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, PHY_CTRL);
  387. if (!(val & PHY_C_RESET))
  388. break;
  389. udelay(50000);
  390. }
  391. if (timeout < 0) {
  392. printf("%s phy reset timeout\n", __func__);
  393. return -1;
  394. }
  395. /* Advertise 100/10 baseT full/half duplex */
  396. sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_ANA,
  397. (PHY_A_FDX|PHY_A_HDX|PHY_A_10FDX|PHY_A_10HDX|PHY_A_EXT));
  398. /* Autonegotiation, normal operation, full duplex, enable tx */
  399. sh_eth_mii_write_phy_reg(port, port_info->phy_addr, PHY_CTRL,
  400. (PHY_C_ANEGEN|PHY_C_RANEG));
  401. /* Wait for autonegotiation to complete */
  402. timeout = 100;
  403. while (timeout--) {
  404. val = sh_eth_mii_read_phy_reg(port, port_info->phy_addr, 1);
  405. if (val & PHY_S_ANEGC)
  406. break;
  407. udelay(50000);
  408. }
  409. if (timeout < 0) {
  410. printf("sh_eth_phy_config() phy auto-negotiation failed\n");
  411. return -1;
  412. }
  413. return 0;
  414. }
  415. static int sh_eth_config(struct dev_info_s *dev, bd_t * bd)
  416. {
  417. int port = dev->port;
  418. struct port_info_s *port_info = &dev->port_info[port];
  419. u32 val;
  420. u32 phy_status;
  421. int rc;
  422. /* Configure e-dmac registers */
  423. outl((inl(EDMR(port)) & ~EMDR_DESC_R) | EDMR_EL, EDMR(port));
  424. outl(0, EESIPR(port));
  425. outl(0, TRSCER(port));
  426. outl(0, TFTR(port));
  427. outl((FIFO_SIZE_T | FIFO_SIZE_R), FDR(port));
  428. outl(RMCR_RST, RMCR(port));
  429. outl(0, RPADIR(port));
  430. outl((FIFO_F_D_RFF | FIFO_F_D_RFD), FCFTR(port));
  431. /* Configure e-mac registers */
  432. outl(0, ECSIPR(port));
  433. /* Set Mac address */
  434. val = bd->bi_enetaddr[0] << 24 | bd->bi_enetaddr[1] << 16 |
  435. bd->bi_enetaddr[2] << 8 | bd->bi_enetaddr[3];
  436. outl(val, MAHR(port));
  437. val = bd->bi_enetaddr[4] << 8 | bd->bi_enetaddr[5];
  438. outl(val, MALR(port));
  439. outl(RFLR_RFL_MIN, RFLR(port));
  440. outl(0, PIPR(port));
  441. outl(APR_AP, APR(port));
  442. outl(MPR_MP, MPR(port));
  443. outl(TPAUSER_TPAUSE, TPAUSER(port));
  444. /* Configure phy */
  445. if ((rc = sh_eth_phy_config(dev)))
  446. return rc;
  447. /* Read phy status to finish configuring the e-mac */
  448. phy_status = sh_eth_mii_read_phy_reg(dev->port,
  449. dev->port_info[dev->port].phy_addr,
  450. 1);
  451. /* Set the transfer speed */
  452. if (phy_status & (PHY_S_100X_F|PHY_S_100X_H)) {
  453. printf("100Base/");
  454. outl(GECMR_100B, GECMR(port));
  455. } else {
  456. printf("10Base/");
  457. outl(GECMR_10B, GECMR(port));
  458. }
  459. /* Check if full duplex mode is supported by the phy */
  460. if (phy_status & (PHY_S_100X_F|PHY_S_10T_F)) {
  461. printf("Full\n");
  462. outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE|ECMR_DM), ECMR(port));
  463. } else {
  464. printf("Half\n");
  465. outl((ECMR_CHG_DM|ECMR_RE|ECMR_TE), ECMR(port));
  466. }
  467. return 0;
  468. }
  469. static int sh_eth_start(struct dev_info_s *dev)
  470. {
  471. /*
  472. * Enable the e-dmac receiver only. The transmitter will be enabled when
  473. * we have something to transmit
  474. */
  475. outl(EDRRR_R, EDRRR(dev->port));
  476. return 0;
  477. }
  478. static int sh_eth_get_mac(bd_t *bd)
  479. {
  480. char *s, *e;
  481. int i;
  482. s = getenv("ethaddr");
  483. if (s != NULL) {
  484. for (i = 0; i < 6; ++i) {
  485. bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
  486. if (s)
  487. s = (*e) ? e + 1 : e;
  488. }
  489. } else {
  490. puts("Please set MAC address\n");
  491. }
  492. return 0;
  493. }
  494. int eth_init(bd_t *bd)
  495. {
  496. int rc;
  497. /* Allocate main device information structure */
  498. if (!(dev = malloc(sizeof(*dev)))) {
  499. printf("eth_init: malloc failed\n");
  500. return -ENOMEM;
  501. }
  502. memset(dev, 0, sizeof(*dev));
  503. dev->port = CONFIG_SH_ETHER_USE_PORT;
  504. dev->port_info[dev->port].phy_addr = CONFIG_SH_ETHER_PHY_ADDR;
  505. sh_eth_get_mac(bd);
  506. if ((rc = sh_eth_reset(dev)) || (rc = sh_eth_desc_init(dev)))
  507. goto err;
  508. if ((rc = sh_eth_config(dev, bd)) || (rc = sh_eth_start(dev)))
  509. goto err_desc;
  510. return 0;
  511. err_desc:
  512. sh_eth_desc_free(dev);
  513. err:
  514. free(dev);
  515. printf("eth_init: Failed\n");
  516. return rc;
  517. }