smc911x.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * SMSC LAN9[12]1[567] Network driver
  3. *
  4. * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #ifdef CONFIG_DRIVER_SMC911X
  26. #include <command.h>
  27. #include <net.h>
  28. #include <miiphy.h>
  29. #ifdef CONFIG_DRIVER_SMC911X_32_BIT
  30. static inline u32 reg_read(u32 addr)
  31. {
  32. return *(volatile u32*)addr;
  33. }
  34. static inline void reg_write(u32 addr, u32 val)
  35. {
  36. *(volatile u32*)addr = val;
  37. }
  38. #else
  39. #error "SMC911X: Only 32-bit bus is supported"
  40. #endif
  41. #define mdelay(n) udelay((n)*1000)
  42. /* Below are the register offsets and bit definitions
  43. * of the Lan911x memory space
  44. */
  45. #define RX_DATA_FIFO (CONFIG_DRIVER_SMC911X_BASE + 0x00)
  46. #define TX_DATA_FIFO (CONFIG_DRIVER_SMC911X_BASE + 0x20)
  47. #define TX_CMD_A_INT_ON_COMP 0x80000000
  48. #define TX_CMD_A_INT_BUF_END_ALGN 0x03000000
  49. #define TX_CMD_A_INT_4_BYTE_ALGN 0x00000000
  50. #define TX_CMD_A_INT_16_BYTE_ALGN 0x01000000
  51. #define TX_CMD_A_INT_32_BYTE_ALGN 0x02000000
  52. #define TX_CMD_A_INT_DATA_OFFSET 0x001F0000
  53. #define TX_CMD_A_INT_FIRST_SEG 0x00002000
  54. #define TX_CMD_A_INT_LAST_SEG 0x00001000
  55. #define TX_CMD_A_BUF_SIZE 0x000007FF
  56. #define TX_CMD_B_PKT_TAG 0xFFFF0000
  57. #define TX_CMD_B_ADD_CRC_DISABLE 0x00002000
  58. #define TX_CMD_B_DISABLE_PADDING 0x00001000
  59. #define TX_CMD_B_PKT_BYTE_LENGTH 0x000007FF
  60. #define RX_STATUS_FIFO (CONFIG_DRIVER_SMC911X_BASE + 0x40)
  61. #define RX_STS_PKT_LEN 0x3FFF0000
  62. #define RX_STS_ES 0x00008000
  63. #define RX_STS_BCST 0x00002000
  64. #define RX_STS_LEN_ERR 0x00001000
  65. #define RX_STS_RUNT_ERR 0x00000800
  66. #define RX_STS_MCAST 0x00000400
  67. #define RX_STS_TOO_LONG 0x00000080
  68. #define RX_STS_COLL 0x00000040
  69. #define RX_STS_ETH_TYPE 0x00000020
  70. #define RX_STS_WDOG_TMT 0x00000010
  71. #define RX_STS_MII_ERR 0x00000008
  72. #define RX_STS_DRIBBLING 0x00000004
  73. #define RX_STS_CRC_ERR 0x00000002
  74. #define RX_STATUS_FIFO_PEEK (CONFIG_DRIVER_SMC911X_BASE + 0x44)
  75. #define TX_STATUS_FIFO (CONFIG_DRIVER_SMC911X_BASE + 0x48)
  76. #define TX_STS_TAG 0xFFFF0000
  77. #define TX_STS_ES 0x00008000
  78. #define TX_STS_LOC 0x00000800
  79. #define TX_STS_NO_CARR 0x00000400
  80. #define TX_STS_LATE_COLL 0x00000200
  81. #define TX_STS_MANY_COLL 0x00000100
  82. #define TX_STS_COLL_CNT 0x00000078
  83. #define TX_STS_MANY_DEFER 0x00000004
  84. #define TX_STS_UNDERRUN 0x00000002
  85. #define TX_STS_DEFERRED 0x00000001
  86. #define TX_STATUS_FIFO_PEEK (CONFIG_DRIVER_SMC911X_BASE + 0x4C)
  87. #define ID_REV (CONFIG_DRIVER_SMC911X_BASE + 0x50)
  88. #define ID_REV_CHIP_ID 0xFFFF0000 /* RO */
  89. #define ID_REV_REV_ID 0x0000FFFF /* RO */
  90. #define INT_CFG (CONFIG_DRIVER_SMC911X_BASE + 0x54)
  91. #define INT_CFG_INT_DEAS 0xFF000000 /* R/W */
  92. #define INT_CFG_INT_DEAS_CLR 0x00004000
  93. #define INT_CFG_INT_DEAS_STS 0x00002000
  94. #define INT_CFG_IRQ_INT 0x00001000 /* RO */
  95. #define INT_CFG_IRQ_EN 0x00000100 /* R/W */
  96. #define INT_CFG_IRQ_POL 0x00000010 /* R/W Not Affected by SW Reset */
  97. #define INT_CFG_IRQ_TYPE 0x00000001 /* R/W Not Affected by SW Reset */
  98. #define INT_STS (CONFIG_DRIVER_SMC911X_BASE + 0x58)
  99. #define INT_STS_SW_INT 0x80000000 /* R/WC */
  100. #define INT_STS_TXSTOP_INT 0x02000000 /* R/WC */
  101. #define INT_STS_RXSTOP_INT 0x01000000 /* R/WC */
  102. #define INT_STS_RXDFH_INT 0x00800000 /* R/WC */
  103. #define INT_STS_RXDF_INT 0x00400000 /* R/WC */
  104. #define INT_STS_TX_IOC 0x00200000 /* R/WC */
  105. #define INT_STS_RXD_INT 0x00100000 /* R/WC */
  106. #define INT_STS_GPT_INT 0x00080000 /* R/WC */
  107. #define INT_STS_PHY_INT 0x00040000 /* RO */
  108. #define INT_STS_PME_INT 0x00020000 /* R/WC */
  109. #define INT_STS_TXSO 0x00010000 /* R/WC */
  110. #define INT_STS_RWT 0x00008000 /* R/WC */
  111. #define INT_STS_RXE 0x00004000 /* R/WC */
  112. #define INT_STS_TXE 0x00002000 /* R/WC */
  113. /*#define INT_STS_ERX 0x00001000*/ /* R/WC */
  114. #define INT_STS_TDFU 0x00000800 /* R/WC */
  115. #define INT_STS_TDFO 0x00000400 /* R/WC */
  116. #define INT_STS_TDFA 0x00000200 /* R/WC */
  117. #define INT_STS_TSFF 0x00000100 /* R/WC */
  118. #define INT_STS_TSFL 0x00000080 /* R/WC */
  119. /*#define INT_STS_RXDF 0x00000040*/ /* R/WC */
  120. #define INT_STS_RDFO 0x00000040 /* R/WC */
  121. #define INT_STS_RDFL 0x00000020 /* R/WC */
  122. #define INT_STS_RSFF 0x00000010 /* R/WC */
  123. #define INT_STS_RSFL 0x00000008 /* R/WC */
  124. #define INT_STS_GPIO2_INT 0x00000004 /* R/WC */
  125. #define INT_STS_GPIO1_INT 0x00000002 /* R/WC */
  126. #define INT_STS_GPIO0_INT 0x00000001 /* R/WC */
  127. #define INT_EN (CONFIG_DRIVER_SMC911X_BASE + 0x5C)
  128. #define INT_EN_SW_INT_EN 0x80000000 /* R/W */
  129. #define INT_EN_TXSTOP_INT_EN 0x02000000 /* R/W */
  130. #define INT_EN_RXSTOP_INT_EN 0x01000000 /* R/W */
  131. #define INT_EN_RXDFH_INT_EN 0x00800000 /* R/W */
  132. /*#define INT_EN_RXDF_INT_EN 0x00400000*/ /* R/W */
  133. #define INT_EN_TIOC_INT_EN 0x00200000 /* R/W */
  134. #define INT_EN_RXD_INT_EN 0x00100000 /* R/W */
  135. #define INT_EN_GPT_INT_EN 0x00080000 /* R/W */
  136. #define INT_EN_PHY_INT_EN 0x00040000 /* R/W */
  137. #define INT_EN_PME_INT_EN 0x00020000 /* R/W */
  138. #define INT_EN_TXSO_EN 0x00010000 /* R/W */
  139. #define INT_EN_RWT_EN 0x00008000 /* R/W */
  140. #define INT_EN_RXE_EN 0x00004000 /* R/W */
  141. #define INT_EN_TXE_EN 0x00002000 /* R/W */
  142. /*#define INT_EN_ERX_EN 0x00001000*/ /* R/W */
  143. #define INT_EN_TDFU_EN 0x00000800 /* R/W */
  144. #define INT_EN_TDFO_EN 0x00000400 /* R/W */
  145. #define INT_EN_TDFA_EN 0x00000200 /* R/W */
  146. #define INT_EN_TSFF_EN 0x00000100 /* R/W */
  147. #define INT_EN_TSFL_EN 0x00000080 /* R/W */
  148. /*#define INT_EN_RXDF_EN 0x00000040*/ /* R/W */
  149. #define INT_EN_RDFO_EN 0x00000040 /* R/W */
  150. #define INT_EN_RDFL_EN 0x00000020 /* R/W */
  151. #define INT_EN_RSFF_EN 0x00000010 /* R/W */
  152. #define INT_EN_RSFL_EN 0x00000008 /* R/W */
  153. #define INT_EN_GPIO2_INT 0x00000004 /* R/W */
  154. #define INT_EN_GPIO1_INT 0x00000002 /* R/W */
  155. #define INT_EN_GPIO0_INT 0x00000001 /* R/W */
  156. #define BYTE_TEST (CONFIG_DRIVER_SMC911X_BASE + 0x64)
  157. #define FIFO_INT (CONFIG_DRIVER_SMC911X_BASE + 0x68)
  158. #define FIFO_INT_TX_AVAIL_LEVEL 0xFF000000 /* R/W */
  159. #define FIFO_INT_TX_STS_LEVEL 0x00FF0000 /* R/W */
  160. #define FIFO_INT_RX_AVAIL_LEVEL 0x0000FF00 /* R/W */
  161. #define FIFO_INT_RX_STS_LEVEL 0x000000FF /* R/W */
  162. #define RX_CFG (CONFIG_DRIVER_SMC911X_BASE + 0x6C)
  163. #define RX_CFG_RX_END_ALGN 0xC0000000 /* R/W */
  164. #define RX_CFG_RX_END_ALGN4 0x00000000 /* R/W */
  165. #define RX_CFG_RX_END_ALGN16 0x40000000 /* R/W */
  166. #define RX_CFG_RX_END_ALGN32 0x80000000 /* R/W */
  167. #define RX_CFG_RX_DMA_CNT 0x0FFF0000 /* R/W */
  168. #define RX_CFG_RX_DUMP 0x00008000 /* R/W */
  169. #define RX_CFG_RXDOFF 0x00001F00 /* R/W */
  170. /*#define RX_CFG_RXBAD 0x00000001*/ /* R/W */
  171. #define TX_CFG (CONFIG_DRIVER_SMC911X_BASE + 0x70)
  172. /*#define TX_CFG_TX_DMA_LVL 0xE0000000*/ /* R/W */
  173. /*#define TX_CFG_TX_DMA_CNT 0x0FFF0000*/ /* R/W Self Clearing */
  174. #define TX_CFG_TXS_DUMP 0x00008000 /* Self Clearing */
  175. #define TX_CFG_TXD_DUMP 0x00004000 /* Self Clearing */
  176. #define TX_CFG_TXSAO 0x00000004 /* R/W */
  177. #define TX_CFG_TX_ON 0x00000002 /* R/W */
  178. #define TX_CFG_STOP_TX 0x00000001 /* Self Clearing */
  179. #define HW_CFG (CONFIG_DRIVER_SMC911X_BASE + 0x74)
  180. #define HW_CFG_TTM 0x00200000 /* R/W */
  181. #define HW_CFG_SF 0x00100000 /* R/W */
  182. #define HW_CFG_TX_FIF_SZ 0x000F0000 /* R/W */
  183. #define HW_CFG_TR 0x00003000 /* R/W */
  184. #define HW_CFG_PHY_CLK_SEL 0x00000060 /* R/W */
  185. #define HW_CFG_PHY_CLK_SEL_INT_PHY 0x00000000 /* R/W */
  186. #define HW_CFG_PHY_CLK_SEL_EXT_PHY 0x00000020 /* R/W */
  187. #define HW_CFG_PHY_CLK_SEL_CLK_DIS 0x00000040 /* R/W */
  188. #define HW_CFG_SMI_SEL 0x00000010 /* R/W */
  189. #define HW_CFG_EXT_PHY_DET 0x00000008 /* RO */
  190. #define HW_CFG_EXT_PHY_EN 0x00000004 /* R/W */
  191. #define HW_CFG_32_16_BIT_MODE 0x00000004 /* RO */
  192. #define HW_CFG_SRST_TO 0x00000002 /* RO */
  193. #define HW_CFG_SRST 0x00000001 /* Self Clearing */
  194. #define RX_DP_CTRL (CONFIG_DRIVER_SMC911X_BASE + 0x78)
  195. #define RX_DP_CTRL_RX_FFWD 0x80000000 /* R/W */
  196. #define RX_DP_CTRL_FFWD_BUSY 0x80000000 /* RO */
  197. #define RX_FIFO_INF (CONFIG_DRIVER_SMC911X_BASE + 0x7C)
  198. #define RX_FIFO_INF_RXSUSED 0x00FF0000 /* RO */
  199. #define RX_FIFO_INF_RXDUSED 0x0000FFFF /* RO */
  200. #define TX_FIFO_INF (CONFIG_DRIVER_SMC911X_BASE + 0x80)
  201. #define TX_FIFO_INF_TSUSED 0x00FF0000 /* RO */
  202. #define TX_FIFO_INF_TDFREE 0x0000FFFF /* RO */
  203. #define PMT_CTRL (CONFIG_DRIVER_SMC911X_BASE + 0x84)
  204. #define PMT_CTRL_PM_MODE 0x00003000 /* Self Clearing */
  205. #define PMT_CTRL_PHY_RST 0x00000400 /* Self Clearing */
  206. #define PMT_CTRL_WOL_EN 0x00000200 /* R/W */
  207. #define PMT_CTRL_ED_EN 0x00000100 /* R/W */
  208. #define PMT_CTRL_PME_TYPE 0x00000040 /* R/W Not Affected by SW Reset */
  209. #define PMT_CTRL_WUPS 0x00000030 /* R/WC */
  210. #define PMT_CTRL_WUPS_NOWAKE 0x00000000 /* R/WC */
  211. #define PMT_CTRL_WUPS_ED 0x00000010 /* R/WC */
  212. #define PMT_CTRL_WUPS_WOL 0x00000020 /* R/WC */
  213. #define PMT_CTRL_WUPS_MULTI 0x00000030 /* R/WC */
  214. #define PMT_CTRL_PME_IND 0x00000008 /* R/W */
  215. #define PMT_CTRL_PME_POL 0x00000004 /* R/W */
  216. #define PMT_CTRL_PME_EN 0x00000002 /* R/W Not Affected by SW Reset */
  217. #define PMT_CTRL_READY 0x00000001 /* RO */
  218. #define GPIO_CFG (CONFIG_DRIVER_SMC911X_BASE + 0x88)
  219. #define GPIO_CFG_LED3_EN 0x40000000 /* R/W */
  220. #define GPIO_CFG_LED2_EN 0x20000000 /* R/W */
  221. #define GPIO_CFG_LED1_EN 0x10000000 /* R/W */
  222. #define GPIO_CFG_GPIO2_INT_POL 0x04000000 /* R/W */
  223. #define GPIO_CFG_GPIO1_INT_POL 0x02000000 /* R/W */
  224. #define GPIO_CFG_GPIO0_INT_POL 0x01000000 /* R/W */
  225. #define GPIO_CFG_EEPR_EN 0x00700000 /* R/W */
  226. #define GPIO_CFG_GPIOBUF2 0x00040000 /* R/W */
  227. #define GPIO_CFG_GPIOBUF1 0x00020000 /* R/W */
  228. #define GPIO_CFG_GPIOBUF0 0x00010000 /* R/W */
  229. #define GPIO_CFG_GPIODIR2 0x00000400 /* R/W */
  230. #define GPIO_CFG_GPIODIR1 0x00000200 /* R/W */
  231. #define GPIO_CFG_GPIODIR0 0x00000100 /* R/W */
  232. #define GPIO_CFG_GPIOD4 0x00000010 /* R/W */
  233. #define GPIO_CFG_GPIOD3 0x00000008 /* R/W */
  234. #define GPIO_CFG_GPIOD2 0x00000004 /* R/W */
  235. #define GPIO_CFG_GPIOD1 0x00000002 /* R/W */
  236. #define GPIO_CFG_GPIOD0 0x00000001 /* R/W */
  237. #define GPT_CFG (CONFIG_DRIVER_SMC911X_BASE + 0x8C)
  238. #define GPT_CFG_TIMER_EN 0x20000000 /* R/W */
  239. #define GPT_CFG_GPT_LOAD 0x0000FFFF /* R/W */
  240. #define GPT_CNT (CONFIG_DRIVER_SMC911X_BASE + 0x90)
  241. #define GPT_CNT_GPT_CNT 0x0000FFFF /* RO */
  242. #define ENDIAN (CONFIG_DRIVER_SMC911X_BASE + 0x98)
  243. #define FREE_RUN (CONFIG_DRIVER_SMC911X_BASE + 0x9C)
  244. #define RX_DROP (CONFIG_DRIVER_SMC911X_BASE + 0xA0)
  245. #define MAC_CSR_CMD (CONFIG_DRIVER_SMC911X_BASE + 0xA4)
  246. #define MAC_CSR_CMD_CSR_BUSY 0x80000000 /* Self Clearing */
  247. #define MAC_CSR_CMD_R_NOT_W 0x40000000 /* R/W */
  248. #define MAC_CSR_CMD_CSR_ADDR 0x000000FF /* R/W */
  249. #define MAC_CSR_DATA (CONFIG_DRIVER_SMC911X_BASE + 0xA8)
  250. #define AFC_CFG (CONFIG_DRIVER_SMC911X_BASE + 0xAC)
  251. #define AFC_CFG_AFC_HI 0x00FF0000 /* R/W */
  252. #define AFC_CFG_AFC_LO 0x0000FF00 /* R/W */
  253. #define AFC_CFG_BACK_DUR 0x000000F0 /* R/W */
  254. #define AFC_CFG_FCMULT 0x00000008 /* R/W */
  255. #define AFC_CFG_FCBRD 0x00000004 /* R/W */
  256. #define AFC_CFG_FCADD 0x00000002 /* R/W */
  257. #define AFC_CFG_FCANY 0x00000001 /* R/W */
  258. #define E2P_CMD (CONFIG_DRIVER_SMC911X_BASE + 0xB0)
  259. #define E2P_CMD_EPC_BUSY 0x80000000 /* Self Clearing */
  260. #define E2P_CMD_EPC_CMD 0x70000000 /* R/W */
  261. #define E2P_CMD_EPC_CMD_READ 0x00000000 /* R/W */
  262. #define E2P_CMD_EPC_CMD_EWDS 0x10000000 /* R/W */
  263. #define E2P_CMD_EPC_CMD_EWEN 0x20000000 /* R/W */
  264. #define E2P_CMD_EPC_CMD_WRITE 0x30000000 /* R/W */
  265. #define E2P_CMD_EPC_CMD_WRAL 0x40000000 /* R/W */
  266. #define E2P_CMD_EPC_CMD_ERASE 0x50000000 /* R/W */
  267. #define E2P_CMD_EPC_CMD_ERAL 0x60000000 /* R/W */
  268. #define E2P_CMD_EPC_CMD_RELOAD 0x70000000 /* R/W */
  269. #define E2P_CMD_EPC_TIMEOUT 0x00000200 /* RO */
  270. #define E2P_CMD_MAC_ADDR_LOADED 0x00000100 /* RO */
  271. #define E2P_CMD_EPC_ADDR 0x000000FF /* R/W */
  272. #define E2P_DATA (CONFIG_DRIVER_SMC911X_BASE + 0xB4)
  273. #define E2P_DATA_EEPROM_DATA 0x000000FF /* R/W */
  274. /* end of LAN register offsets and bit definitions */
  275. /* MAC Control and Status registers */
  276. #define MAC_CR 0x01 /* R/W */
  277. /* MAC_CR - MAC Control Register */
  278. #define MAC_CR_RXALL 0x80000000
  279. /* TODO: delete this bit? It is not described in the data sheet. */
  280. #define MAC_CR_HBDIS 0x10000000
  281. #define MAC_CR_RCVOWN 0x00800000
  282. #define MAC_CR_LOOPBK 0x00200000
  283. #define MAC_CR_FDPX 0x00100000
  284. #define MAC_CR_MCPAS 0x00080000
  285. #define MAC_CR_PRMS 0x00040000
  286. #define MAC_CR_INVFILT 0x00020000
  287. #define MAC_CR_PASSBAD 0x00010000
  288. #define MAC_CR_HFILT 0x00008000
  289. #define MAC_CR_HPFILT 0x00002000
  290. #define MAC_CR_LCOLL 0x00001000
  291. #define MAC_CR_BCAST 0x00000800
  292. #define MAC_CR_DISRTY 0x00000400
  293. #define MAC_CR_PADSTR 0x00000100
  294. #define MAC_CR_BOLMT_MASK 0x000000C0
  295. #define MAC_CR_DFCHK 0x00000020
  296. #define MAC_CR_TXEN 0x00000008
  297. #define MAC_CR_RXEN 0x00000004
  298. #define ADDRH 0x02 /* R/W mask 0x0000FFFFUL */
  299. #define ADDRL 0x03 /* R/W mask 0xFFFFFFFFUL */
  300. #define HASHH 0x04 /* R/W */
  301. #define HASHL 0x05 /* R/W */
  302. #define MII_ACC 0x06 /* R/W */
  303. #define MII_ACC_PHY_ADDR 0x0000F800
  304. #define MII_ACC_MIIRINDA 0x000007C0
  305. #define MII_ACC_MII_WRITE 0x00000002
  306. #define MII_ACC_MII_BUSY 0x00000001
  307. #define MII_DATA 0x07 /* R/W mask 0x0000FFFFUL */
  308. #define FLOW 0x08 /* R/W */
  309. #define FLOW_FCPT 0xFFFF0000
  310. #define FLOW_FCPASS 0x00000004
  311. #define FLOW_FCEN 0x00000002
  312. #define FLOW_FCBSY 0x00000001
  313. #define VLAN1 0x09 /* R/W mask 0x0000FFFFUL */
  314. #define VLAN1_VTI1 0x0000ffff
  315. #define VLAN2 0x0A /* R/W mask 0x0000FFFFUL */
  316. #define VLAN2_VTI2 0x0000ffff
  317. #define WUFF 0x0B /* WO */
  318. #define WUCSR 0x0C /* R/W */
  319. #define WUCSR_GUE 0x00000200
  320. #define WUCSR_WUFR 0x00000040
  321. #define WUCSR_MPR 0x00000020
  322. #define WUCSR_WAKE_EN 0x00000004
  323. #define WUCSR_MPEN 0x00000002
  324. /* Chip ID values */
  325. #define CHIP_9115 0x115
  326. #define CHIP_9116 0x116
  327. #define CHIP_9117 0x117
  328. #define CHIP_9118 0x118
  329. #define CHIP_9215 0x115a
  330. #define CHIP_9216 0x116a
  331. #define CHIP_9217 0x117a
  332. #define CHIP_9218 0x118a
  333. struct chip_id {
  334. u16 id;
  335. char *name;
  336. };
  337. static const struct chip_id chip_ids[] = {
  338. { CHIP_9115, "LAN9115" },
  339. { CHIP_9116, "LAN9116" },
  340. { CHIP_9117, "LAN9117" },
  341. { CHIP_9118, "LAN9118" },
  342. { CHIP_9215, "LAN9215" },
  343. { CHIP_9216, "LAN9216" },
  344. { CHIP_9217, "LAN9217" },
  345. { CHIP_9218, "LAN9218" },
  346. { 0, NULL },
  347. };
  348. #define DRIVERNAME "smc911x"
  349. u32 smc911x_get_mac_csr(u8 reg)
  350. {
  351. while (reg_read(MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
  352. ;
  353. reg_write(MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
  354. while (reg_read(MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
  355. ;
  356. return reg_read(MAC_CSR_DATA);
  357. }
  358. void smc911x_set_mac_csr(u8 reg, u32 data)
  359. {
  360. while (reg_read(MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
  361. ;
  362. reg_write(MAC_CSR_DATA, data);
  363. reg_write(MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
  364. while (reg_read(MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
  365. ;
  366. }
  367. static int smx911x_handle_mac_address(bd_t *bd)
  368. {
  369. unsigned long addrh, addrl;
  370. unsigned char *m = bd->bi_enetaddr;
  371. /* if the environment has a valid mac address then use it */
  372. if ((m[0] | m[1] | m[2] | m[3] | m[4] | m[5])) {
  373. addrl = m[0] | m[1] << 8 | m[2] << 16 | m[3] << 24;
  374. addrh = m[4] | m[5] << 8;
  375. smc911x_set_mac_csr(ADDRH, addrh);
  376. smc911x_set_mac_csr(ADDRL, addrl);
  377. } else {
  378. /* if not, try to get one from the eeprom */
  379. addrh = smc911x_get_mac_csr(ADDRH);
  380. addrl = smc911x_get_mac_csr(ADDRL);
  381. m[0] = (addrl ) & 0xff;
  382. m[1] = (addrl >> 8 ) & 0xff;
  383. m[2] = (addrl >> 16 ) & 0xff;
  384. m[3] = (addrl >> 24 ) & 0xff;
  385. m[4] = (addrh ) & 0xff;
  386. m[5] = (addrh >> 8 ) & 0xff;
  387. /* we get 0xff when there is no eeprom connected */
  388. if ((m[0] & m[1] & m[2] & m[3] & m[4] & m[5]) == 0xff) {
  389. printf(DRIVERNAME ": no valid mac address in environment "
  390. "and no eeprom found\n");
  391. return -1;
  392. }
  393. }
  394. printf(DRIVERNAME ": MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  395. m[0], m[1], m[2], m[3], m[4], m[5]);
  396. return 0;
  397. }
  398. static int smc911x_miiphy_read(u8 phy, u8 reg, u16 *val)
  399. {
  400. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  401. ;
  402. smc911x_set_mac_csr(MII_ACC, phy << 11 | reg << 6 | MII_ACC_MII_BUSY);
  403. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  404. ;
  405. *val = smc911x_get_mac_csr(MII_DATA);
  406. return 0;
  407. }
  408. static int smc911x_miiphy_write(u8 phy, u8 reg, u16 val)
  409. {
  410. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  411. ;
  412. smc911x_set_mac_csr(MII_DATA, val);
  413. smc911x_set_mac_csr(MII_ACC,
  414. phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE);
  415. while (smc911x_get_mac_csr(MII_ACC) & MII_ACC_MII_BUSY)
  416. ;
  417. return 0;
  418. }
  419. static int smc911x_phy_reset(void)
  420. {
  421. u32 reg;
  422. reg = reg_read(PMT_CTRL);
  423. reg &= ~0xfffff030;
  424. reg |= PMT_CTRL_PHY_RST;
  425. reg_write(PMT_CTRL, reg);
  426. mdelay(100);
  427. return 0;
  428. }
  429. static void smc911x_phy_configure(void)
  430. {
  431. int timeout;
  432. u16 status;
  433. smc911x_phy_reset();
  434. smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_RESET);
  435. mdelay(1);
  436. smc911x_miiphy_write(1, PHY_ANAR, 0x01e1);
  437. smc911x_miiphy_write(1, PHY_BMCR, PHY_BMCR_AUTON | PHY_BMCR_RST_NEG);
  438. timeout = 5000;
  439. do {
  440. mdelay(1);
  441. if ((timeout--) == 0)
  442. goto err_out;
  443. if (smc911x_miiphy_read(1, PHY_BMSR, &status) != 0)
  444. goto err_out;
  445. } while (!(status & PHY_BMSR_LS));
  446. printf(DRIVERNAME ": phy initialized\n");
  447. return;
  448. err_out:
  449. printf(DRIVERNAME ": autonegotiation timed out\n");
  450. }
  451. static void smc911x_reset(void)
  452. {
  453. int timeout;
  454. /* Take out of PM setting first */
  455. if (reg_read(PMT_CTRL) & PMT_CTRL_READY) {
  456. /* Write to the bytetest will take out of powerdown */
  457. reg_write(BYTE_TEST, 0x0);
  458. timeout = 10;
  459. while (timeout-- && !(reg_read(PMT_CTRL) & PMT_CTRL_READY))
  460. udelay(10);
  461. if (!timeout) {
  462. printf(DRIVERNAME
  463. ": timeout waiting for PM restore\n");
  464. return;
  465. }
  466. }
  467. /* Disable interrupts */
  468. reg_write(INT_EN, 0);
  469. reg_write(HW_CFG, HW_CFG_SRST);
  470. timeout = 1000;
  471. while (timeout-- && reg_read(E2P_CMD) & E2P_CMD_EPC_BUSY)
  472. udelay(10);
  473. if (!timeout) {
  474. printf(DRIVERNAME ": reset timeout\n");
  475. return;
  476. }
  477. /* Reset the FIFO level and flow control settings */
  478. smc911x_set_mac_csr(FLOW, FLOW_FCPT | FLOW_FCEN);
  479. reg_write(AFC_CFG, 0x0050287F);
  480. /* Set to LED outputs */
  481. reg_write(GPIO_CFG, 0x70070000);
  482. }
  483. static void smc911x_enable(void)
  484. {
  485. /* Enable TX */
  486. reg_write(HW_CFG, 8 << 16 | HW_CFG_SF);
  487. reg_write(GPT_CFG, GPT_CFG_TIMER_EN | 10000);
  488. reg_write(TX_CFG, TX_CFG_TX_ON);
  489. /* no padding to start of packets */
  490. reg_write(RX_CFG, 0);
  491. smc911x_set_mac_csr(MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | MAC_CR_HBDIS);
  492. }
  493. int eth_init(bd_t *bd)
  494. {
  495. unsigned long val, i;
  496. printf(DRIVERNAME ": initializing\n");
  497. val = reg_read(BYTE_TEST);
  498. if (val != 0x87654321) {
  499. printf(DRIVERNAME ": Invalid chip endian 0x08%x\n", val);
  500. goto err_out;
  501. }
  502. val = reg_read(ID_REV) >> 16;
  503. for (i = 0; chip_ids[i].id != 0; i++) {
  504. if (chip_ids[i].id == val) break;
  505. }
  506. if (!chip_ids[i].id) {
  507. printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
  508. goto err_out;
  509. }
  510. printf(DRIVERNAME ": detected %s controller\n", chip_ids[i].name);
  511. smc911x_reset();
  512. /* Configure the PHY, initialize the link state */
  513. smc911x_phy_configure();
  514. if (smx911x_handle_mac_address(bd))
  515. goto err_out;
  516. /* Turn on Tx + Rx */
  517. smc911x_enable();
  518. return 0;
  519. err_out:
  520. return -1;
  521. }
  522. int eth_send(volatile void *packet, int length)
  523. {
  524. u32 *data = (u32*)packet;
  525. u32 tmplen;
  526. u32 status;
  527. reg_write(TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | TX_CMD_A_INT_LAST_SEG | length);
  528. reg_write(TX_DATA_FIFO, length);
  529. tmplen = (length + 3) / 4;
  530. while (tmplen--)
  531. reg_write(TX_DATA_FIFO, *data++);
  532. /* wait for transmission */
  533. while (!((reg_read(TX_FIFO_INF) & TX_FIFO_INF_TSUSED) >> 16));
  534. /* get status. Ignore 'no carrier' error, it has no meaning for
  535. * full duplex operation
  536. */
  537. status = reg_read(TX_STATUS_FIFO) & (TX_STS_LOC | TX_STS_LATE_COLL |
  538. TX_STS_MANY_COLL | TX_STS_MANY_DEFER | TX_STS_UNDERRUN);
  539. if (!status)
  540. return 0;
  541. printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n",
  542. status & TX_STS_LOC ? "TX_STS_LOC " : "",
  543. status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "",
  544. status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "",
  545. status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "",
  546. status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : "");
  547. return -1;
  548. }
  549. void eth_halt(void)
  550. {
  551. smc911x_reset();
  552. }
  553. int eth_rx(void)
  554. {
  555. u32 *data = (u32 *)NetRxPackets[0];
  556. u32 pktlen, tmplen;
  557. u32 status;
  558. if ((reg_read(RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) {
  559. status = reg_read(RX_STATUS_FIFO);
  560. pktlen = (status & RX_STS_PKT_LEN) >> 16;
  561. reg_write(RX_CFG, 0);
  562. tmplen = (pktlen + 2+ 3) / 4;
  563. while (tmplen--)
  564. *data++ = reg_read(RX_DATA_FIFO);
  565. if (status & RX_STS_ES)
  566. printf(DRIVERNAME
  567. ": dropped bad packet. Status: 0x%08x\n",
  568. status);
  569. else
  570. NetReceive(NetRxPackets[0], pktlen);
  571. }
  572. return 0;
  573. }
  574. #endif /* CONFIG_DRIVER_SMC911X */