Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * PS3 virtual uart |
| 3 | * |
| 4 | * Copyright (C) 2006 Sony Computer Entertainment Inc. |
| 5 | * Copyright 2006 Sony Corp. |
| 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; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/interrupt.h> |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 24 | #include <linux/workqueue.h> |
Jiri Slaby | 1977f03 | 2007-10-18 23:40:25 -0700 | [diff] [blame] | 25 | #include <linux/bitops.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 26 | #include <asm/ps3.h> |
| 27 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 28 | #include <asm/firmware.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 29 | #include <asm/lv1call.h> |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 30 | |
| 31 | #include "vuart.h" |
| 32 | |
| 33 | MODULE_AUTHOR("Sony Corporation"); |
| 34 | MODULE_LICENSE("GPL v2"); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 35 | MODULE_DESCRIPTION("PS3 vuart"); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * vuart - An inter-partition data link service. |
| 39 | * port 0: PS3 AV Settings. |
| 40 | * port 2: PS3 System Manager. |
| 41 | * |
| 42 | * The vuart provides a bi-directional byte stream data link between logical |
| 43 | * partitions. Its primary role is as a communications link between the guest |
| 44 | * OS and the system policy module. The current HV does not support any |
| 45 | * connections other than those listed. |
| 46 | */ |
| 47 | |
| 48 | enum {PORT_COUNT = 3,}; |
| 49 | |
| 50 | enum vuart_param { |
| 51 | PARAM_TX_TRIGGER = 0, |
| 52 | PARAM_RX_TRIGGER = 1, |
| 53 | PARAM_INTERRUPT_MASK = 2, |
| 54 | PARAM_RX_BUF_SIZE = 3, /* read only */ |
| 55 | PARAM_RX_BYTES = 4, /* read only */ |
| 56 | PARAM_TX_BUF_SIZE = 5, /* read only */ |
| 57 | PARAM_TX_BYTES = 6, /* read only */ |
| 58 | PARAM_INTERRUPT_STATUS = 7, /* read only */ |
| 59 | }; |
| 60 | |
| 61 | enum vuart_interrupt_bit { |
| 62 | INTERRUPT_BIT_TX = 0, |
| 63 | INTERRUPT_BIT_RX = 1, |
| 64 | INTERRUPT_BIT_DISCONNECT = 2, |
| 65 | }; |
| 66 | |
| 67 | enum vuart_interrupt_mask { |
| 68 | INTERRUPT_MASK_TX = 1, |
| 69 | INTERRUPT_MASK_RX = 2, |
| 70 | INTERRUPT_MASK_DISCONNECT = 4, |
| 71 | }; |
| 72 | |
| 73 | /** |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 74 | * struct ps3_vuart_port_priv - private vuart device data. |
| 75 | */ |
| 76 | |
| 77 | struct ps3_vuart_port_priv { |
| 78 | u64 interrupt_mask; |
| 79 | |
| 80 | struct { |
| 81 | spinlock_t lock; |
| 82 | struct list_head head; |
| 83 | } tx_list; |
| 84 | struct { |
| 85 | struct ps3_vuart_work work; |
| 86 | unsigned long bytes_held; |
| 87 | spinlock_t lock; |
| 88 | struct list_head head; |
| 89 | } rx_list; |
| 90 | struct ps3_vuart_stats stats; |
| 91 | }; |
| 92 | |
| 93 | static struct ps3_vuart_port_priv *to_port_priv( |
| 94 | struct ps3_system_bus_device *dev) |
| 95 | { |
| 96 | BUG_ON(!dev); |
| 97 | BUG_ON(!dev->driver_priv); |
| 98 | return (struct ps3_vuart_port_priv *)dev->driver_priv; |
| 99 | } |
| 100 | |
| 101 | /** |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 102 | * struct ports_bmp - bitmap indicating ports needing service. |
| 103 | * |
| 104 | * A 256 bit read only bitmap indicating ports needing service. Do not write |
| 105 | * to these bits. Must not cross a page boundary. |
| 106 | */ |
| 107 | |
| 108 | struct ports_bmp { |
| 109 | u64 status; |
| 110 | u64 unused[3]; |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 111 | } __attribute__((aligned(32))); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 112 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 113 | #define dump_ports_bmp(_b) _dump_ports_bmp(_b, __func__, __LINE__) |
Geoff Levand | 848cfdc | 2007-06-16 07:18:14 +1000 | [diff] [blame] | 114 | static void __maybe_unused _dump_ports_bmp( |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 115 | const struct ports_bmp *bmp, const char *func, int line) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 116 | { |
| 117 | pr_debug("%s:%d: ports_bmp: %016lxh\n", func, line, bmp->status); |
| 118 | } |
| 119 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 120 | #define dump_port_params(_b) _dump_port_params(_b, __func__, __LINE__) |
Geoff Levand | 848cfdc | 2007-06-16 07:18:14 +1000 | [diff] [blame] | 121 | static void __maybe_unused _dump_port_params(unsigned int port_number, |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 122 | const char *func, int line) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 123 | { |
| 124 | #if defined(DEBUG) |
| 125 | static const char *strings[] = { |
| 126 | "tx_trigger ", |
| 127 | "rx_trigger ", |
| 128 | "interrupt_mask ", |
| 129 | "rx_buf_size ", |
| 130 | "rx_bytes ", |
| 131 | "tx_buf_size ", |
| 132 | "tx_bytes ", |
| 133 | "interrupt_status", |
| 134 | }; |
| 135 | int result; |
| 136 | unsigned int i; |
| 137 | u64 value; |
| 138 | |
| 139 | for (i = 0; i < ARRAY_SIZE(strings); i++) { |
| 140 | result = lv1_get_virtual_uart_param(port_number, i, &value); |
| 141 | |
| 142 | if (result) { |
| 143 | pr_debug("%s:%d: port_%u: %s failed: %s\n", func, line, |
| 144 | port_number, strings[i], ps3_result(result)); |
| 145 | continue; |
| 146 | } |
| 147 | pr_debug("%s:%d: port_%u: %s = %lxh\n", |
| 148 | func, line, port_number, strings[i], value); |
| 149 | } |
| 150 | #endif |
| 151 | } |
| 152 | |
| 153 | struct vuart_triggers { |
| 154 | unsigned long rx; |
| 155 | unsigned long tx; |
| 156 | }; |
| 157 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 158 | int ps3_vuart_get_triggers(struct ps3_system_bus_device *dev, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 159 | struct vuart_triggers *trig) |
| 160 | { |
| 161 | int result; |
| 162 | unsigned long size; |
| 163 | unsigned long val; |
| 164 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 165 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 166 | PARAM_TX_TRIGGER, &trig->tx); |
| 167 | |
| 168 | if (result) { |
| 169 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", |
| 170 | __func__, __LINE__, ps3_result(result)); |
| 171 | return result; |
| 172 | } |
| 173 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 174 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 175 | PARAM_RX_BUF_SIZE, &size); |
| 176 | |
| 177 | if (result) { |
| 178 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", |
| 179 | __func__, __LINE__, ps3_result(result)); |
| 180 | return result; |
| 181 | } |
| 182 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 183 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 184 | PARAM_RX_TRIGGER, &val); |
| 185 | |
| 186 | if (result) { |
| 187 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", |
| 188 | __func__, __LINE__, ps3_result(result)); |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | trig->rx = size - val; |
| 193 | |
| 194 | dev_dbg(&dev->core, "%s:%d: tx %lxh, rx %lxh\n", __func__, __LINE__, |
| 195 | trig->tx, trig->rx); |
| 196 | |
| 197 | return result; |
| 198 | } |
| 199 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 200 | int ps3_vuart_set_triggers(struct ps3_system_bus_device *dev, unsigned int tx, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 201 | unsigned int rx) |
| 202 | { |
| 203 | int result; |
| 204 | unsigned long size; |
| 205 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 206 | result = lv1_set_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 207 | PARAM_TX_TRIGGER, tx); |
| 208 | |
| 209 | if (result) { |
| 210 | dev_dbg(&dev->core, "%s:%d: tx_trigger failed: %s\n", |
| 211 | __func__, __LINE__, ps3_result(result)); |
| 212 | return result; |
| 213 | } |
| 214 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 215 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 216 | PARAM_RX_BUF_SIZE, &size); |
| 217 | |
| 218 | if (result) { |
| 219 | dev_dbg(&dev->core, "%s:%d: tx_buf_size failed: %s\n", |
| 220 | __func__, __LINE__, ps3_result(result)); |
| 221 | return result; |
| 222 | } |
| 223 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 224 | result = lv1_set_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 225 | PARAM_RX_TRIGGER, size - rx); |
| 226 | |
| 227 | if (result) { |
| 228 | dev_dbg(&dev->core, "%s:%d: rx_trigger failed: %s\n", |
| 229 | __func__, __LINE__, ps3_result(result)); |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | dev_dbg(&dev->core, "%s:%d: tx %xh, rx %xh\n", __func__, __LINE__, |
| 234 | tx, rx); |
| 235 | |
| 236 | return result; |
| 237 | } |
| 238 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 239 | static int ps3_vuart_get_rx_bytes_waiting(struct ps3_system_bus_device *dev, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 240 | u64 *bytes_waiting) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 241 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 242 | int result; |
| 243 | |
| 244 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 245 | PARAM_RX_BYTES, bytes_waiting); |
| 246 | |
| 247 | if (result) |
| 248 | dev_dbg(&dev->core, "%s:%d: rx_bytes failed: %s\n", |
| 249 | __func__, __LINE__, ps3_result(result)); |
| 250 | |
| 251 | dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, |
| 252 | *bytes_waiting); |
| 253 | return result; |
| 254 | } |
| 255 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 256 | /** |
| 257 | * ps3_vuart_set_interrupt_mask - Enable/disable the port interrupt sources. |
| 258 | * @dev: The struct ps3_system_bus_device instance. |
| 259 | * @bmp: Logical OR of enum vuart_interrupt_mask values. A zero bit disables. |
| 260 | */ |
| 261 | |
| 262 | static int ps3_vuart_set_interrupt_mask(struct ps3_system_bus_device *dev, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 263 | unsigned long mask) |
| 264 | { |
| 265 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 266 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 267 | |
| 268 | dev_dbg(&dev->core, "%s:%d: %lxh\n", __func__, __LINE__, mask); |
| 269 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 270 | priv->interrupt_mask = mask; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 271 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 272 | result = lv1_set_virtual_uart_param(dev->port_number, |
| 273 | PARAM_INTERRUPT_MASK, priv->interrupt_mask); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 274 | |
| 275 | if (result) |
| 276 | dev_dbg(&dev->core, "%s:%d: interrupt_mask failed: %s\n", |
| 277 | __func__, __LINE__, ps3_result(result)); |
| 278 | |
| 279 | return result; |
| 280 | } |
| 281 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 282 | static int ps3_vuart_get_interrupt_status(struct ps3_system_bus_device *dev, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 283 | unsigned long *status) |
| 284 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 285 | int result; |
| 286 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 287 | u64 tmp; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 288 | |
| 289 | result = lv1_get_virtual_uart_param(dev->port_number, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 290 | PARAM_INTERRUPT_STATUS, &tmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 291 | |
| 292 | if (result) |
| 293 | dev_dbg(&dev->core, "%s:%d: interrupt_status failed: %s\n", |
| 294 | __func__, __LINE__, ps3_result(result)); |
| 295 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 296 | *status = tmp & priv->interrupt_mask; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 297 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 298 | dev_dbg(&dev->core, "%s:%d: m %lxh, s %lxh, m&s %lxh\n", |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 299 | __func__, __LINE__, priv->interrupt_mask, tmp, *status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 300 | |
| 301 | return result; |
| 302 | } |
| 303 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 304 | int ps3_vuart_enable_interrupt_tx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 305 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 306 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 307 | |
| 308 | return (priv->interrupt_mask & INTERRUPT_MASK_TX) ? 0 |
| 309 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 310 | | INTERRUPT_MASK_TX); |
| 311 | } |
| 312 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 313 | int ps3_vuart_enable_interrupt_rx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 314 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 315 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 316 | |
| 317 | return (priv->interrupt_mask & INTERRUPT_MASK_RX) ? 0 |
| 318 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 319 | | INTERRUPT_MASK_RX); |
| 320 | } |
| 321 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 322 | int ps3_vuart_enable_interrupt_disconnect(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 323 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 324 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 325 | |
| 326 | return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) ? 0 |
| 327 | : ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 328 | | INTERRUPT_MASK_DISCONNECT); |
| 329 | } |
| 330 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 331 | int ps3_vuart_disable_interrupt_tx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 332 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 333 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 334 | |
| 335 | return (priv->interrupt_mask & INTERRUPT_MASK_TX) |
| 336 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 337 | & ~INTERRUPT_MASK_TX) : 0; |
| 338 | } |
| 339 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 340 | int ps3_vuart_disable_interrupt_rx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 341 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 342 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 343 | |
| 344 | return (priv->interrupt_mask & INTERRUPT_MASK_RX) |
| 345 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 346 | & ~INTERRUPT_MASK_RX) : 0; |
| 347 | } |
| 348 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 349 | int ps3_vuart_disable_interrupt_disconnect(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 350 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 351 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 352 | |
| 353 | return (priv->interrupt_mask & INTERRUPT_MASK_DISCONNECT) |
| 354 | ? ps3_vuart_set_interrupt_mask(dev, priv->interrupt_mask |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 355 | & ~INTERRUPT_MASK_DISCONNECT) : 0; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * ps3_vuart_raw_write - Low level write helper. |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 360 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 361 | * |
| 362 | * Do not call ps3_vuart_raw_write directly, use ps3_vuart_write. |
| 363 | */ |
| 364 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 365 | static int ps3_vuart_raw_write(struct ps3_system_bus_device *dev, |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 366 | const void *buf, unsigned int bytes, unsigned long *bytes_written) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 367 | { |
| 368 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 369 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 370 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 371 | result = lv1_write_virtual_uart(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 372 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_written); |
| 373 | |
| 374 | if (result) { |
| 375 | dev_dbg(&dev->core, "%s:%d: lv1_write_virtual_uart failed: " |
| 376 | "%s\n", __func__, __LINE__, ps3_result(result)); |
| 377 | return result; |
| 378 | } |
| 379 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 380 | priv->stats.bytes_written += *bytes_written; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 381 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 382 | dev_dbg(&dev->core, "%s:%d: wrote %lxh/%xh=>%lxh\n", __func__, __LINE__, |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 383 | *bytes_written, bytes, priv->stats.bytes_written); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 384 | |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * ps3_vuart_raw_read - Low level read helper. |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 390 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 391 | * |
| 392 | * Do not call ps3_vuart_raw_read directly, use ps3_vuart_read. |
| 393 | */ |
| 394 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 395 | static int ps3_vuart_raw_read(struct ps3_system_bus_device *dev, void *buf, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 396 | unsigned int bytes, unsigned long *bytes_read) |
| 397 | { |
| 398 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 399 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 400 | |
| 401 | dev_dbg(&dev->core, "%s:%d: %xh\n", __func__, __LINE__, bytes); |
| 402 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 403 | result = lv1_read_virtual_uart(dev->port_number, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 404 | ps3_mm_phys_to_lpar(__pa(buf)), bytes, bytes_read); |
| 405 | |
| 406 | if (result) { |
| 407 | dev_dbg(&dev->core, "%s:%d: lv1_read_virtual_uart failed: %s\n", |
| 408 | __func__, __LINE__, ps3_result(result)); |
| 409 | return result; |
| 410 | } |
| 411 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 412 | priv->stats.bytes_read += *bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 413 | |
| 414 | dev_dbg(&dev->core, "%s:%d: read %lxh/%xh=>%lxh\n", __func__, __LINE__, |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 415 | *bytes_read, bytes, priv->stats.bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 416 | |
| 417 | return result; |
| 418 | } |
| 419 | |
| 420 | /** |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 421 | * ps3_vuart_clear_rx_bytes - Discard bytes received. |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 422 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 423 | * @bytes: Max byte count to discard, zero = all pending. |
| 424 | * |
| 425 | * Used to clear pending rx interrupt source. Will not block. |
| 426 | */ |
| 427 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 428 | void ps3_vuart_clear_rx_bytes(struct ps3_system_bus_device *dev, |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 429 | unsigned int bytes) |
| 430 | { |
| 431 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 432 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 433 | u64 bytes_waiting; |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 434 | void *tmp; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 435 | |
| 436 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes_waiting); |
| 437 | |
| 438 | BUG_ON(result); |
| 439 | |
| 440 | bytes = bytes ? min(bytes, (unsigned int)bytes_waiting) : bytes_waiting; |
| 441 | |
| 442 | dev_dbg(&dev->core, "%s:%d: %u\n", __func__, __LINE__, bytes); |
| 443 | |
| 444 | if (!bytes) |
| 445 | return; |
| 446 | |
| 447 | /* Add some extra space for recently arrived data. */ |
| 448 | |
| 449 | bytes += 128; |
| 450 | |
| 451 | tmp = kmalloc(bytes, GFP_KERNEL); |
| 452 | |
| 453 | if (!tmp) |
| 454 | return; |
| 455 | |
| 456 | ps3_vuart_raw_read(dev, tmp, bytes, &bytes_waiting); |
| 457 | |
| 458 | kfree(tmp); |
| 459 | |
| 460 | /* Don't include these bytes in the stats. */ |
| 461 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 462 | priv->stats.bytes_read -= bytes_waiting; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 463 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 464 | EXPORT_SYMBOL_GPL(ps3_vuart_clear_rx_bytes); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 465 | |
| 466 | /** |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 467 | * struct list_buffer - An element for a port device fifo buffer list. |
| 468 | */ |
| 469 | |
| 470 | struct list_buffer { |
| 471 | struct list_head link; |
| 472 | const unsigned char *head; |
| 473 | const unsigned char *tail; |
| 474 | unsigned long dbg_number; |
| 475 | unsigned char data[]; |
| 476 | }; |
| 477 | |
| 478 | /** |
| 479 | * ps3_vuart_write - the entry point for writing data to a port |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 480 | * @dev: The struct ps3_system_bus_device instance. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 481 | * |
| 482 | * If the port is idle on entry as much of the incoming data is written to |
| 483 | * the port as the port will accept. Otherwise a list buffer is created |
| 484 | * and any remaning incoming data is copied to that buffer. The buffer is |
| 485 | * then enqueued for transmision via the transmit interrupt. |
| 486 | */ |
| 487 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 488 | int ps3_vuart_write(struct ps3_system_bus_device *dev, const void *buf, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 489 | unsigned int bytes) |
| 490 | { |
| 491 | static unsigned long dbg_number; |
| 492 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 493 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 494 | unsigned long flags; |
| 495 | struct list_buffer *lb; |
| 496 | |
| 497 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, |
| 498 | bytes, bytes); |
| 499 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 500 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 501 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 502 | if (list_empty(&priv->tx_list.head)) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 503 | unsigned long bytes_written; |
| 504 | |
| 505 | result = ps3_vuart_raw_write(dev, buf, bytes, &bytes_written); |
| 506 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 507 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 508 | |
| 509 | if (result) { |
| 510 | dev_dbg(&dev->core, |
| 511 | "%s:%d: ps3_vuart_raw_write failed\n", |
| 512 | __func__, __LINE__); |
| 513 | return result; |
| 514 | } |
| 515 | |
| 516 | if (bytes_written == bytes) { |
| 517 | dev_dbg(&dev->core, "%s:%d: wrote %xh bytes\n", |
| 518 | __func__, __LINE__, bytes); |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | bytes -= bytes_written; |
| 523 | buf += bytes_written; |
| 524 | } else |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 525 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 526 | |
| 527 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_KERNEL); |
| 528 | |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 529 | if (!lb) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 530 | return -ENOMEM; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 531 | |
| 532 | memcpy(lb->data, buf, bytes); |
| 533 | lb->head = lb->data; |
| 534 | lb->tail = lb->data + bytes; |
| 535 | lb->dbg_number = ++dbg_number; |
| 536 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 537 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
| 538 | list_add_tail(&lb->link, &priv->tx_list.head); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 539 | ps3_vuart_enable_interrupt_tx(dev); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 540 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 541 | |
| 542 | dev_dbg(&dev->core, "%s:%d: queued buf_%lu, %xh bytes\n", |
| 543 | __func__, __LINE__, lb->dbg_number, bytes); |
| 544 | |
| 545 | return 0; |
| 546 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 547 | EXPORT_SYMBOL_GPL(ps3_vuart_write); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 548 | |
| 549 | /** |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 550 | * ps3_vuart_queue_rx_bytes - Queue waiting bytes into the buffer list. |
| 551 | * @dev: The struct ps3_system_bus_device instance. |
| 552 | * @bytes_queued: Number of bytes queued to the buffer list. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 553 | * |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 554 | * Must be called with priv->rx_list.lock held. |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 555 | */ |
| 556 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 557 | static int ps3_vuart_queue_rx_bytes(struct ps3_system_bus_device *dev, |
| 558 | u64 *bytes_queued) |
| 559 | { |
| 560 | static unsigned long dbg_number; |
| 561 | int result; |
| 562 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 563 | struct list_buffer *lb; |
| 564 | u64 bytes; |
| 565 | |
| 566 | *bytes_queued = 0; |
| 567 | |
| 568 | result = ps3_vuart_get_rx_bytes_waiting(dev, &bytes); |
| 569 | BUG_ON(result); |
| 570 | |
| 571 | if (result) |
| 572 | return -EIO; |
| 573 | |
| 574 | if (!bytes) |
| 575 | return 0; |
| 576 | |
| 577 | /* Add some extra space for recently arrived data. */ |
| 578 | |
| 579 | bytes += 128; |
| 580 | |
| 581 | lb = kmalloc(sizeof(struct list_buffer) + bytes, GFP_ATOMIC); |
| 582 | |
| 583 | if (!lb) |
| 584 | return -ENOMEM; |
| 585 | |
| 586 | ps3_vuart_raw_read(dev, lb->data, bytes, &bytes); |
| 587 | |
| 588 | lb->head = lb->data; |
| 589 | lb->tail = lb->data + bytes; |
| 590 | lb->dbg_number = ++dbg_number; |
| 591 | |
| 592 | list_add_tail(&lb->link, &priv->rx_list.head); |
| 593 | priv->rx_list.bytes_held += bytes; |
| 594 | |
| 595 | dev_dbg(&dev->core, "%s:%d: buf_%lu: queued %lxh bytes\n", |
| 596 | __func__, __LINE__, lb->dbg_number, bytes); |
| 597 | |
| 598 | *bytes_queued = bytes; |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * ps3_vuart_read - The entry point for reading data from a port. |
| 605 | * |
| 606 | * Queue data waiting at the port, and if enough bytes to satisfy the request |
| 607 | * are held in the buffer list those bytes are dequeued and copied to the |
| 608 | * caller's buffer. Emptied list buffers are retiered. If the request cannot |
| 609 | * be statified by bytes held in the list buffers -EAGAIN is returned. |
| 610 | */ |
| 611 | |
| 612 | int ps3_vuart_read(struct ps3_system_bus_device *dev, void *buf, |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 613 | unsigned int bytes) |
| 614 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 615 | int result; |
| 616 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 617 | unsigned long flags; |
| 618 | struct list_buffer *lb, *n; |
| 619 | unsigned long bytes_read; |
| 620 | |
| 621 | dev_dbg(&dev->core, "%s:%d: %u(%xh) bytes\n", __func__, __LINE__, |
| 622 | bytes, bytes); |
| 623 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 624 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 625 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 626 | /* Queue rx bytes here for polled reads. */ |
| 627 | |
| 628 | while (priv->rx_list.bytes_held < bytes) { |
| 629 | u64 tmp; |
| 630 | |
| 631 | result = ps3_vuart_queue_rx_bytes(dev, &tmp); |
| 632 | if (result || !tmp) { |
| 633 | dev_dbg(&dev->core, "%s:%d: starved for %lxh bytes\n", |
| 634 | __func__, __LINE__, |
| 635 | bytes - priv->rx_list.bytes_held); |
| 636 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
| 637 | return -EAGAIN; |
| 638 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 639 | } |
| 640 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 641 | list_for_each_entry_safe(lb, n, &priv->rx_list.head, link) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 642 | bytes_read = min((unsigned int)(lb->tail - lb->head), bytes); |
| 643 | |
| 644 | memcpy(buf, lb->head, bytes_read); |
| 645 | buf += bytes_read; |
| 646 | bytes -= bytes_read; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 647 | priv->rx_list.bytes_held -= bytes_read; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 648 | |
| 649 | if (bytes_read < lb->tail - lb->head) { |
| 650 | lb->head += bytes_read; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 651 | dev_dbg(&dev->core, "%s:%d: buf_%lu: dequeued %lxh " |
| 652 | "bytes\n", __func__, __LINE__, lb->dbg_number, |
| 653 | bytes_read); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 654 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 658 | dev_dbg(&dev->core, "%s:%d: buf_%lu: free, dequeued %lxh " |
| 659 | "bytes\n", __func__, __LINE__, lb->dbg_number, |
| 660 | bytes_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 661 | |
| 662 | list_del(&lb->link); |
| 663 | kfree(lb); |
| 664 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 665 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 666 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 667 | return 0; |
| 668 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 669 | EXPORT_SYMBOL_GPL(ps3_vuart_read); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 670 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 671 | /** |
| 672 | * ps3_vuart_work - Asynchronous read handler. |
| 673 | */ |
| 674 | |
| 675 | static void ps3_vuart_work(struct work_struct *work) |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 676 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 677 | struct ps3_system_bus_device *dev = |
| 678 | ps3_vuart_work_to_system_bus_dev(work); |
| 679 | struct ps3_vuart_port_driver *drv = |
| 680 | ps3_system_bus_dev_to_vuart_drv(dev); |
| 681 | |
| 682 | BUG_ON(!drv); |
| 683 | drv->work(dev); |
| 684 | } |
| 685 | |
| 686 | int ps3_vuart_read_async(struct ps3_system_bus_device *dev, unsigned int bytes) |
| 687 | { |
| 688 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 689 | unsigned long flags; |
| 690 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 691 | if (priv->rx_list.work.trigger) { |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 692 | dev_dbg(&dev->core, "%s:%d: warning, multiple calls\n", |
| 693 | __func__, __LINE__); |
| 694 | return -EAGAIN; |
| 695 | } |
| 696 | |
| 697 | BUG_ON(!bytes); |
| 698 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 699 | PREPARE_WORK(&priv->rx_list.work.work, ps3_vuart_work); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 700 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 701 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
| 702 | if (priv->rx_list.bytes_held >= bytes) { |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 703 | dev_dbg(&dev->core, "%s:%d: schedule_work %xh bytes\n", |
| 704 | __func__, __LINE__, bytes); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 705 | schedule_work(&priv->rx_list.work.work); |
| 706 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 707 | return 0; |
| 708 | } |
| 709 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 710 | priv->rx_list.work.trigger = bytes; |
| 711 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 712 | |
| 713 | dev_dbg(&dev->core, "%s:%d: waiting for %u(%xh) bytes\n", __func__, |
| 714 | __LINE__, bytes, bytes); |
| 715 | |
| 716 | return 0; |
| 717 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 718 | EXPORT_SYMBOL_GPL(ps3_vuart_read_async); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 719 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 720 | void ps3_vuart_cancel_async(struct ps3_system_bus_device *dev) |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 721 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 722 | to_port_priv(dev)->rx_list.work.trigger = 0; |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 723 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 724 | EXPORT_SYMBOL_GPL(ps3_vuart_cancel_async); |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 725 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 726 | /** |
| 727 | * ps3_vuart_handle_interrupt_tx - third stage transmit interrupt handler |
| 728 | * |
| 729 | * Services the transmit interrupt for the port. Writes as much data from the |
| 730 | * buffer list as the port will accept. Retires any emptied list buffers and |
| 731 | * adjusts the final list buffer state for a partial write. |
| 732 | */ |
| 733 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 734 | static int ps3_vuart_handle_interrupt_tx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 735 | { |
| 736 | int result = 0; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 737 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 738 | unsigned long flags; |
| 739 | struct list_buffer *lb, *n; |
| 740 | unsigned long bytes_total = 0; |
| 741 | |
| 742 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 743 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 744 | spin_lock_irqsave(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 745 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 746 | list_for_each_entry_safe(lb, n, &priv->tx_list.head, link) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 747 | |
| 748 | unsigned long bytes_written; |
| 749 | |
| 750 | result = ps3_vuart_raw_write(dev, lb->head, lb->tail - lb->head, |
| 751 | &bytes_written); |
| 752 | |
| 753 | if (result) { |
| 754 | dev_dbg(&dev->core, |
| 755 | "%s:%d: ps3_vuart_raw_write failed\n", |
| 756 | __func__, __LINE__); |
| 757 | break; |
| 758 | } |
| 759 | |
| 760 | bytes_total += bytes_written; |
| 761 | |
| 762 | if (bytes_written < lb->tail - lb->head) { |
| 763 | lb->head += bytes_written; |
| 764 | dev_dbg(&dev->core, |
| 765 | "%s:%d cleared buf_%lu, %lxh bytes\n", |
| 766 | __func__, __LINE__, lb->dbg_number, |
| 767 | bytes_written); |
| 768 | goto port_full; |
| 769 | } |
| 770 | |
| 771 | dev_dbg(&dev->core, "%s:%d free buf_%lu\n", __func__, __LINE__, |
| 772 | lb->dbg_number); |
| 773 | |
| 774 | list_del(&lb->link); |
| 775 | kfree(lb); |
| 776 | } |
| 777 | |
| 778 | ps3_vuart_disable_interrupt_tx(dev); |
| 779 | port_full: |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 780 | spin_unlock_irqrestore(&priv->tx_list.lock, flags); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 781 | dev_dbg(&dev->core, "%s:%d wrote %lxh bytes total\n", |
| 782 | __func__, __LINE__, bytes_total); |
| 783 | return result; |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * ps3_vuart_handle_interrupt_rx - third stage receive interrupt handler |
| 788 | * |
| 789 | * Services the receive interrupt for the port. Creates a list buffer and |
| 790 | * copies all waiting port data to that buffer and enqueues the buffer in the |
| 791 | * buffer list. Buffer list data is dequeued via ps3_vuart_read. |
| 792 | */ |
| 793 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 794 | static int ps3_vuart_handle_interrupt_rx(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 795 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 796 | int result; |
| 797 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 798 | unsigned long flags; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 799 | u64 bytes; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 800 | |
| 801 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 802 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 803 | spin_lock_irqsave(&priv->rx_list.lock, flags); |
| 804 | result = ps3_vuart_queue_rx_bytes(dev, &bytes); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 805 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 806 | if (result) { |
| 807 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
| 808 | return result; |
Geoff Levand | ea1547d | 2007-02-06 14:23:47 -0800 | [diff] [blame] | 809 | } |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 810 | |
| 811 | if (priv->rx_list.work.trigger && priv->rx_list.bytes_held |
| 812 | >= priv->rx_list.work.trigger) { |
| 813 | dev_dbg(&dev->core, "%s:%d: schedule_work %lxh bytes\n", |
| 814 | __func__, __LINE__, priv->rx_list.work.trigger); |
| 815 | priv->rx_list.work.trigger = 0; |
| 816 | schedule_work(&priv->rx_list.work.work); |
| 817 | } |
| 818 | |
| 819 | spin_unlock_irqrestore(&priv->rx_list.lock, flags); |
| 820 | return result; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | static int ps3_vuart_handle_interrupt_disconnect( |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 824 | struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 825 | { |
| 826 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 827 | BUG_ON("no support"); |
| 828 | return -1; |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * ps3_vuart_handle_port_interrupt - second stage interrupt handler |
| 833 | * |
| 834 | * Services any pending interrupt types for the port. Passes control to the |
| 835 | * third stage type specific interrupt handler. Returns control to the first |
| 836 | * stage handler after one iteration. |
| 837 | */ |
| 838 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 839 | static int ps3_vuart_handle_port_interrupt(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 840 | { |
| 841 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 842 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 843 | unsigned long status; |
| 844 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 845 | result = ps3_vuart_get_interrupt_status(dev, &status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 846 | |
| 847 | if (result) |
| 848 | return result; |
| 849 | |
| 850 | dev_dbg(&dev->core, "%s:%d: status: %lxh\n", __func__, __LINE__, |
| 851 | status); |
| 852 | |
| 853 | if (status & INTERRUPT_MASK_DISCONNECT) { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 854 | priv->stats.disconnect_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 855 | result = ps3_vuart_handle_interrupt_disconnect(dev); |
| 856 | if (result) |
| 857 | ps3_vuart_disable_interrupt_disconnect(dev); |
| 858 | } |
| 859 | |
| 860 | if (status & INTERRUPT_MASK_TX) { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 861 | priv->stats.tx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 862 | result = ps3_vuart_handle_interrupt_tx(dev); |
| 863 | if (result) |
| 864 | ps3_vuart_disable_interrupt_tx(dev); |
| 865 | } |
| 866 | |
| 867 | if (status & INTERRUPT_MASK_RX) { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 868 | priv->stats.rx_interrupts++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 869 | result = ps3_vuart_handle_interrupt_rx(dev); |
| 870 | if (result) |
| 871 | ps3_vuart_disable_interrupt_rx(dev); |
| 872 | } |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 877 | struct vuart_bus_priv { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 878 | struct ports_bmp *bmp; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 879 | unsigned int virq; |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 880 | struct mutex probe_mutex; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 881 | int use_count; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 882 | struct ps3_system_bus_device *devices[PORT_COUNT]; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 883 | } static vuart_bus_priv; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 884 | |
| 885 | /** |
| 886 | * ps3_vuart_irq_handler - first stage interrupt handler |
| 887 | * |
| 888 | * Loops finding any interrupting port and its associated instance data. |
| 889 | * Passes control to the second stage port specific interrupt handler. Loops |
| 890 | * until all outstanding interrupts are serviced. |
| 891 | */ |
| 892 | |
| 893 | static irqreturn_t ps3_vuart_irq_handler(int irq, void *_private) |
| 894 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 895 | struct vuart_bus_priv *bus_priv = _private; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 896 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 897 | BUG_ON(!bus_priv); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 898 | |
| 899 | while (1) { |
| 900 | unsigned int port; |
| 901 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 902 | dump_ports_bmp(bus_priv->bmp); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 903 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 904 | port = (BITS_PER_LONG - 1) - __ilog2(bus_priv->bmp->status); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 905 | |
| 906 | if (port == BITS_PER_LONG) |
| 907 | break; |
| 908 | |
| 909 | BUG_ON(port >= PORT_COUNT); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 910 | BUG_ON(!bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 911 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 912 | ps3_vuart_handle_port_interrupt(bus_priv->devices[port]); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | return IRQ_HANDLED; |
| 916 | } |
| 917 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 918 | static int ps3_vuart_bus_interrupt_get(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 919 | { |
| 920 | int result; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 921 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 922 | pr_debug(" -> %s:%d\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 923 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 924 | vuart_bus_priv.use_count++; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 925 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 926 | BUG_ON(vuart_bus_priv.use_count > 2); |
| 927 | |
Geert Uytterhoeven | d0e5c218 | 2008-01-19 07:32:19 +1100 | [diff] [blame] | 928 | if (vuart_bus_priv.use_count != 1) |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 929 | return 0; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 930 | |
| 931 | BUG_ON(vuart_bus_priv.bmp); |
| 932 | |
| 933 | vuart_bus_priv.bmp = kzalloc(sizeof(struct ports_bmp), GFP_KERNEL); |
| 934 | |
| 935 | if (!vuart_bus_priv.bmp) { |
| 936 | pr_debug("%s:%d: kzalloc failed.\n", __func__, __LINE__); |
| 937 | result = -ENOMEM; |
| 938 | goto fail_bmp_malloc; |
| 939 | } |
| 940 | |
| 941 | result = ps3_vuart_irq_setup(PS3_BINDING_CPU_ANY, vuart_bus_priv.bmp, |
| 942 | &vuart_bus_priv.virq); |
| 943 | |
| 944 | if (result) { |
| 945 | pr_debug("%s:%d: ps3_vuart_irq_setup failed (%d)\n", |
| 946 | __func__, __LINE__, result); |
| 947 | result = -EPERM; |
| 948 | goto fail_alloc_irq; |
| 949 | } |
| 950 | |
| 951 | result = request_irq(vuart_bus_priv.virq, ps3_vuart_irq_handler, |
| 952 | IRQF_DISABLED, "vuart", &vuart_bus_priv); |
| 953 | |
| 954 | if (result) { |
| 955 | pr_debug("%s:%d: request_irq failed (%d)\n", |
| 956 | __func__, __LINE__, result); |
| 957 | goto fail_request_irq; |
| 958 | } |
| 959 | |
| 960 | pr_debug(" <- %s:%d: ok\n", __func__, __LINE__); |
| 961 | return result; |
| 962 | |
| 963 | fail_request_irq: |
| 964 | ps3_vuart_irq_destroy(vuart_bus_priv.virq); |
| 965 | vuart_bus_priv.virq = NO_IRQ; |
| 966 | fail_alloc_irq: |
| 967 | kfree(vuart_bus_priv.bmp); |
| 968 | vuart_bus_priv.bmp = NULL; |
| 969 | fail_bmp_malloc: |
| 970 | vuart_bus_priv.use_count--; |
| 971 | pr_debug(" <- %s:%d: failed\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 972 | return result; |
| 973 | } |
| 974 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 975 | static int ps3_vuart_bus_interrupt_put(void) |
| 976 | { |
| 977 | pr_debug(" -> %s:%d\n", __func__, __LINE__); |
| 978 | |
| 979 | vuart_bus_priv.use_count--; |
| 980 | |
| 981 | BUG_ON(vuart_bus_priv.use_count < 0); |
| 982 | |
| 983 | if (vuart_bus_priv.use_count != 0) |
| 984 | return 0; |
| 985 | |
| 986 | free_irq(vuart_bus_priv.virq, &vuart_bus_priv); |
| 987 | |
| 988 | ps3_vuart_irq_destroy(vuart_bus_priv.virq); |
| 989 | vuart_bus_priv.virq = NO_IRQ; |
| 990 | |
| 991 | kfree(vuart_bus_priv.bmp); |
| 992 | vuart_bus_priv.bmp = NULL; |
| 993 | |
| 994 | pr_debug(" <- %s:%d\n", __func__, __LINE__); |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static int ps3_vuart_probe(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 999 | { |
| 1000 | int result; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1001 | struct ps3_vuart_port_driver *drv; |
| 1002 | struct ps3_vuart_port_priv *priv = NULL; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1003 | |
| 1004 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 1005 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1006 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
| 1007 | |
| 1008 | dev_dbg(&dev->core, "%s:%d: (%s)\n", __func__, __LINE__, |
| 1009 | drv->core.core.name); |
| 1010 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1011 | BUG_ON(!drv); |
| 1012 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1013 | if (dev->port_number >= PORT_COUNT) { |
| 1014 | BUG(); |
| 1015 | return -EINVAL; |
| 1016 | } |
| 1017 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1018 | mutex_lock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1019 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1020 | result = ps3_vuart_bus_interrupt_get(); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1021 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1022 | if (result) |
| 1023 | goto fail_setup_interrupt; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1024 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1025 | if (vuart_bus_priv.devices[dev->port_number]) { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1026 | dev_dbg(&dev->core, "%s:%d: port busy (%d)\n", __func__, |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1027 | __LINE__, dev->port_number); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1028 | result = -EBUSY; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1029 | goto fail_busy; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1032 | vuart_bus_priv.devices[dev->port_number] = dev; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1033 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1034 | /* Setup dev->driver_priv. */ |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1035 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1036 | dev->driver_priv = kzalloc(sizeof(struct ps3_vuart_port_priv), |
| 1037 | GFP_KERNEL); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1038 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1039 | if (!dev->driver_priv) { |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1040 | result = -ENOMEM; |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1041 | goto fail_dev_malloc; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1044 | priv = to_port_priv(dev); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1045 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1046 | INIT_LIST_HEAD(&priv->tx_list.head); |
| 1047 | spin_lock_init(&priv->tx_list.lock); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1048 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1049 | INIT_LIST_HEAD(&priv->rx_list.head); |
| 1050 | spin_lock_init(&priv->rx_list.lock); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1051 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1052 | INIT_WORK(&priv->rx_list.work.work, NULL); |
| 1053 | priv->rx_list.work.trigger = 0; |
| 1054 | priv->rx_list.work.dev = dev; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1055 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1056 | /* clear stale pending interrupts */ |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1057 | |
| 1058 | ps3_vuart_clear_rx_bytes(dev, 0); |
| 1059 | |
| 1060 | ps3_vuart_set_interrupt_mask(dev, INTERRUPT_MASK_RX); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1061 | |
| 1062 | ps3_vuart_set_triggers(dev, 1, 1); |
| 1063 | |
| 1064 | if (drv->probe) |
| 1065 | result = drv->probe(dev); |
| 1066 | else { |
| 1067 | result = 0; |
| 1068 | dev_info(&dev->core, "%s:%d: no probe method\n", __func__, |
| 1069 | __LINE__); |
| 1070 | } |
| 1071 | |
| 1072 | if (result) { |
| 1073 | dev_dbg(&dev->core, "%s:%d: drv->probe failed\n", |
| 1074 | __func__, __LINE__); |
| 1075 | goto fail_probe; |
| 1076 | } |
| 1077 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1078 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1079 | |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1080 | return result; |
| 1081 | |
| 1082 | fail_probe: |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1083 | ps3_vuart_set_interrupt_mask(dev, 0); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1084 | kfree(dev->driver_priv); |
| 1085 | dev->driver_priv = NULL; |
| 1086 | fail_dev_malloc: |
| 1087 | vuart_bus_priv.devices[dev->port_number] = NULL; |
| 1088 | fail_busy: |
| 1089 | ps3_vuart_bus_interrupt_put(); |
| 1090 | fail_setup_interrupt: |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1091 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1092 | dev_dbg(&dev->core, "%s:%d: failed\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1093 | return result; |
| 1094 | } |
| 1095 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1096 | /** |
| 1097 | * ps3_vuart_cleanup - common cleanup helper. |
| 1098 | * @dev: The struct ps3_system_bus_device instance. |
| 1099 | * |
| 1100 | * Cleans interrupts and HV resources. Must be called with |
| 1101 | * vuart_bus_priv.probe_mutex held. Used by ps3_vuart_remove and |
| 1102 | * ps3_vuart_shutdown. After this call, polled reading will still work. |
| 1103 | */ |
| 1104 | |
| 1105 | static int ps3_vuart_cleanup(struct ps3_system_bus_device *dev) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1106 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1107 | dev_dbg(&dev->core, "%s:%d\n", __func__, __LINE__); |
| 1108 | |
| 1109 | ps3_vuart_cancel_async(dev); |
| 1110 | ps3_vuart_set_interrupt_mask(dev, 0); |
| 1111 | ps3_vuart_bus_interrupt_put(); |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * ps3_vuart_remove - Completely clean the device instance. |
| 1117 | * @dev: The struct ps3_system_bus_device instance. |
| 1118 | * |
| 1119 | * Cleans all memory, interrupts and HV resources. After this call the |
| 1120 | * device can no longer be used. |
| 1121 | */ |
| 1122 | |
| 1123 | static int ps3_vuart_remove(struct ps3_system_bus_device *dev) |
| 1124 | { |
| 1125 | struct ps3_vuart_port_priv *priv = to_port_priv(dev); |
| 1126 | struct ps3_vuart_port_driver *drv; |
| 1127 | |
| 1128 | BUG_ON(!dev); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1129 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1130 | mutex_lock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1131 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1132 | dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, |
| 1133 | dev->match_id); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1134 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1135 | if (!dev->core.driver) { |
| 1136 | dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, |
| 1137 | __LINE__); |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1138 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1139 | return 0; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1140 | } |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1141 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1142 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
| 1143 | |
| 1144 | BUG_ON(!drv); |
| 1145 | |
| 1146 | if (drv->remove) { |
| 1147 | drv->remove(dev); |
| 1148 | } else { |
| 1149 | dev_dbg(&dev->core, "%s:%d: no remove method\n", __func__, |
| 1150 | __LINE__); |
| 1151 | BUG(); |
| 1152 | } |
| 1153 | |
| 1154 | ps3_vuart_cleanup(dev); |
| 1155 | |
| 1156 | vuart_bus_priv.devices[dev->port_number] = NULL; |
| 1157 | kfree(priv); |
| 1158 | priv = NULL; |
| 1159 | |
| 1160 | dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1161 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * ps3_vuart_shutdown - Cleans interrupts and HV resources. |
| 1167 | * @dev: The struct ps3_system_bus_device instance. |
| 1168 | * |
| 1169 | * Cleans interrupts and HV resources. After this call the |
| 1170 | * device can still be used in polling mode. This behavior required |
| 1171 | * by sys-manager to be able to complete the device power operation |
| 1172 | * sequence. |
| 1173 | */ |
| 1174 | |
| 1175 | static int ps3_vuart_shutdown(struct ps3_system_bus_device *dev) |
| 1176 | { |
| 1177 | struct ps3_vuart_port_driver *drv; |
| 1178 | |
| 1179 | BUG_ON(!dev); |
| 1180 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1181 | mutex_lock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1182 | |
| 1183 | dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, |
| 1184 | dev->match_id); |
| 1185 | |
| 1186 | if (!dev->core.driver) { |
| 1187 | dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, |
| 1188 | __LINE__); |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1189 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | drv = ps3_system_bus_dev_to_vuart_drv(dev); |
| 1194 | |
| 1195 | BUG_ON(!drv); |
| 1196 | |
| 1197 | if (drv->shutdown) |
| 1198 | drv->shutdown(dev); |
| 1199 | else if (drv->remove) { |
| 1200 | dev_dbg(&dev->core, "%s:%d: no shutdown, calling remove\n", |
| 1201 | __func__, __LINE__); |
| 1202 | drv->remove(dev); |
| 1203 | } else { |
| 1204 | dev_dbg(&dev->core, "%s:%d: no shutdown method\n", __func__, |
| 1205 | __LINE__); |
| 1206 | BUG(); |
| 1207 | } |
| 1208 | |
| 1209 | ps3_vuart_cleanup(dev); |
| 1210 | |
| 1211 | dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1212 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1213 | mutex_unlock(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1214 | return 0; |
| 1215 | } |
| 1216 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1217 | static int __init ps3_vuart_bus_init(void) |
Geert Uytterhoeven | 5b8e8ee | 2007-02-12 00:55:15 -0800 | [diff] [blame] | 1218 | { |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1219 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1220 | |
| 1221 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
Geert Uytterhoeven | ef596c6 | 2007-03-10 00:05:38 +0100 | [diff] [blame] | 1222 | return -ENODEV; |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1223 | |
Geoff Levand | c4e6752 | 2008-01-19 07:32:53 +1100 | [diff] [blame] | 1224 | mutex_init(&vuart_bus_priv.probe_mutex); |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1225 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1226 | return 0; |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1227 | } |
| 1228 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1229 | static void __exit ps3_vuart_bus_exit(void) |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1230 | { |
| 1231 | pr_debug("%s:%d:\n", __func__, __LINE__); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1232 | } |
| 1233 | |
Geoff Levand | 75c86e7 | 2007-02-13 17:37:28 -0800 | [diff] [blame] | 1234 | core_initcall(ps3_vuart_bus_init); |
| 1235 | module_exit(ps3_vuart_bus_exit); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1236 | |
| 1237 | /** |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1238 | * ps3_vuart_port_driver_register - Add a vuart port device driver. |
| 1239 | */ |
| 1240 | |
| 1241 | int ps3_vuart_port_driver_register(struct ps3_vuart_port_driver *drv) |
| 1242 | { |
| 1243 | int result; |
| 1244 | |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1245 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); |
| 1246 | |
| 1247 | BUG_ON(!drv->core.match_id); |
| 1248 | BUG_ON(!drv->core.core.name); |
| 1249 | |
| 1250 | drv->core.probe = ps3_vuart_probe; |
| 1251 | drv->core.remove = ps3_vuart_remove; |
| 1252 | drv->core.shutdown = ps3_vuart_shutdown; |
| 1253 | |
| 1254 | result = ps3_system_bus_driver_register(&drv->core); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1255 | return result; |
| 1256 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1257 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_register); |
| 1258 | |
| 1259 | /** |
| 1260 | * ps3_vuart_port_driver_unregister - Remove a vuart port device driver. |
| 1261 | */ |
| 1262 | |
| 1263 | void ps3_vuart_port_driver_unregister(struct ps3_vuart_port_driver *drv) |
| 1264 | { |
Geoff Levand | 7626e78 | 2007-06-16 08:01:06 +1000 | [diff] [blame] | 1265 | pr_debug("%s:%d: (%s)\n", __func__, __LINE__, drv->core.core.name); |
| 1266 | ps3_system_bus_driver_unregister(&drv->core); |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1267 | } |
Geoff Levand | 74e95d5d | 2006-12-08 18:27:47 -0800 | [diff] [blame] | 1268 | EXPORT_SYMBOL_GPL(ps3_vuart_port_driver_unregister); |