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